存在RewriteCond文件 [英] RewriteCond file exists
问题描述
这是一个简单的问题,但是htaccess和我从来没有相处过。
Such a simple problem but htaccess and I have never got along.
- 提供文件(如果存在)。
- 如果URI只是/并且存在index.html,则提供index.html
- 否则,提供app.php
这里是.htaccess:
Here is .htaccess:
# Disable the directory processing, Apache 2.4
DirectoryIndex disabled xxx
RewriteEngine On
# Serve existing files
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# For / serve index.html if it exists
RewriteCond %{REQUEST_URI} ^/$
RewriteCond index.html -f
RewriteRule .? index.html [L]
# Otherwise use front controller
RewriteRule .? app.php [L]
如果我将RewriteCond index.html -f行注释为只要index.html确实存在。但是我也想检查index.php,所以我需要检查文件是否存在。而且如果没有别的我想了解为什么张贴的行不起作用。
It works if I comment out the RewriteCond index.html -f line as long as index.html does in fact exists. But I also want to check for index.php so I need the file exists check. And if nothing else I'd like to understand why the posted lines do not work.
推荐答案
这应该起作用:
# Disable the directory processing, Apache 2.4
DirectoryIndex disabled xxx
RewriteEngine On
# Serve existing files
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# For / serve index.html if it exists
RewriteCond %{DOCUMENT_ROOT}/index.html -f [NC]
RewriteRule ^/?$ index.html [L]
# Otherwise use front controller
RewriteRule ^ app.php [L]
请记住 RewriteCond
-f
或 -d
的文件或目录需要完整路径,这就是为什么需要在文件名前加上%{DOCUMENT_ROOT} /
。无需在这里转义。
Remember that RewriteCond
with -f
or -d
needs full path to the file or directory that's why you need to prefix the filename with %{DOCUMENT_ROOT}/
. No need to escape dots here.
这篇关于存在RewriteCond文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!