我正在使用我的服务器响应测试404和其他错误,并且404正常工作。
我输入example.com/page
,example.com/directory/
,example.com/directory/page
,它会返回404错误。
然后我尝试example.com/directory/page/string
(比如123或关键词 - 这里),它返回了500个错误。
我正在将此代码用于mod_rewrite
# redirects index.html to /
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ /$1 [R=301,L]
# rewrites file.html to file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
如何将这500个错误转换为正确的404?
答案 0 :(得分:1)
最有可能是它的循环,尝试添加这个条件:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
在你的最终规则中。所以它看起来像这样:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
# redirects index.html to /
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ /$1 [R=301,L]
# rewrites file.html to file
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]