这么简单的问题但我和htaccess却一直相处不来。
- 如果文件存在,则提供该文件。
- 如果 URI 只是 / 并且 index.html 存在,则提供 index.html
- 否则提供 app.php
这是.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,所以我需要文件是否存在检查。如果没有别的事,我想了解为什么发布的行不起作用。
这应该有效:
# 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
with -f
or -d
需要文件或目录的完整路径,这就是为什么您需要在文件名前加上前缀%{DOCUMENT_ROOT}/
。这里不需要转义点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)