.htaccess 配置后显示 500 错误而不是 404
Showing 500 Error instead 404 after .htaccess configuration
我最近在 .htaccess 文件中添加了以下行:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ / [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /.php [L]
我想要达到的目标:
对于 PHP 个文件:
example.com/abc.php should be redirected as example.com/abc
example.com/abc/ should be redirected as example.com/abc
example.com/abc should load as example.com/abc
对于目录,以正常行为加载:
注:xyz是一个目录
example.com/xyz should load as example.com/xyz/
example.com/xyz/ should load as example.com/xyz/
问题是如果有人尝试 example.com/abc/bla,它会抛出 500 错误而不是 404。
有人可以帮忙修改上面的 .htaccess 行来解决 500 错误问题吗?
经过多次更正,我解决了这个问题。
这是 htaccess
的最终代码
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ / [L,R=301]
#Enforce a no-trailing-slash policy.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]
# 404 ANY URL WITH ADDITIONAL PATH INFO ##
RewriteCond %{PATH_INFO} .
RewriteRule ^ - [R=404]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /.php [L]
此代码执行以下操作
example.com/abc.php redirecting as example.com/abc
example.com/abc/ redirecting as example.com/abc
example.com/abc showing as example.com/abc
example.com/abc/nopage redirecting to 404
我最近在 .htaccess 文件中添加了以下行:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ / [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /.php [L]
我想要达到的目标: 对于 PHP 个文件:
example.com/abc.php should be redirected as example.com/abc
example.com/abc/ should be redirected as example.com/abc
example.com/abc should load as example.com/abc
对于目录,以正常行为加载: 注:xyz是一个目录
example.com/xyz should load as example.com/xyz/
example.com/xyz/ should load as example.com/xyz/
问题是如果有人尝试 example.com/abc/bla,它会抛出 500 错误而不是 404。 有人可以帮忙修改上面的 .htaccess 行来解决 500 错误问题吗?
经过多次更正,我解决了这个问题。 这是 htaccess
的最终代码# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ / [L,R=301]
#Enforce a no-trailing-slash policy.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]
# 404 ANY URL WITH ADDITIONAL PATH INFO ##
RewriteCond %{PATH_INFO} .
RewriteRule ^ - [R=404]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /.php [L]
此代码执行以下操作
example.com/abc.php redirecting as example.com/abc
example.com/abc/ redirecting as example.com/abc
example.com/abc showing as example.com/abc
example.com/abc/nopage redirecting to 404