.htaccess 重写规则创建500内部服务器错误而不是404

jm81lzqq  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(155)

我的.htaccess工作,它做我想要的,但当我写一个不存在的文件到地址栏,它给我500内部服务器错误,而不是404找不到.你能帮助我,为什么?

RewriteEngine ON
RewriteCond %{REQUEST_URI} !/?exception\.php [NC]
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php/?\s [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ $1.php [QSA,L]

RewriteCond %{THE_REQUEST} \s/new\.php\?article=(\S+)\s [NC]
RewriteRule ^ /news/%1? [R=301,L]
RewriteRule ^news/([^/]*)$ new.php?article=$1 [NC,QSA,L]

RewriteCond %{THE_REQUEST} \s/category\.php\?catid=(\S+)\s [NC]
RewriteRule ^ /news/categories/%1? [R=301,L]
RewriteRule ^news/categories/([^/]*)$ category.php?catid=$1 [NC,QSA,L]
wvt8vs2t

wvt8vs2t1#

您的第二条规则似乎是为每个不存在的URI写入.php的罪魁祸首。
您可以在站点根目录.htaccess中尝试以下代码:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/category\.php\?catid=(\S+)\s [NC]
RewriteRule ^ /news/categories/%1? [R=301,L]

RewriteCond %{THE_REQUEST} \s/new\.php\?article=(\S+)\s [NC]
RewriteRule ^ /news/%1? [R=301,L]

RewriteCond %{REQUEST_URI} !/(exception\.php|admin) [NC]
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php/?\s [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteRule ^news/categories/([^/]*)$ category.php?catid=$1 [NC,QSA,L]

RewriteRule ^news/([^/]*)$ new.php?article=$1 [NC,QSA,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

请注意最后一条规则,该规则仅为具有匹配的.php文件的URI添加.php扩展名。

相关问题