.htaccess RewriteCond文件存在

toiithl6  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(83)

这样一个简单的问题,但htaccess和我从来没有沿着。
1.如果文件存在,则提供该文件。
1.如果URI只是/,并且如果index.html存在,则提供index.html
1.否则服务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,所以我需要检查文件是否存在。如果没有别的,我想知道为什么张贴线不工作。

tsm1rwdh

tsm1rwdh1#

这应该可以工作:

# 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}/。这里不需要转义点。

相关问题