【问题标题】:Showing 500 Error instead 404 after .htaccess configuration.htaccess 配置后显示 500 错误而不是 404
【发布时间】:2021-05-28 16:26:09
【问题描述】:

我最近在 .htaccess 文件中添加了以下几行:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.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 的任何页面上都收到 500 错误?还是仅在您给出的一个示例中发生?
  • @ShafeequeM,500 内部错误来自服务器,如果您还有其他规则,请告诉我们吗?
  • 注释掉所有行,然后一一添加回来。它会告诉你哪条线路有问题。还要输入LogLevel debug 并检查您的日志文件。 500 是内部服务器错误,因此 apache 不喜欢您的配置中的某些内容。

标签: apache .htaccess


【解决方案1】:

经过多次更正,我解决了这个问题。 这是htaccess的最终代码

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
#Enforce a no-trailing-slash policy.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [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 ^/?(.*)$ /$1.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

【讨论】:

    猜你喜欢
    • 2021-11-29
    • 2010-12-17
    • 2021-12-21
    • 2015-05-26
    • 1970-01-01
    • 2017-06-28
    • 2014-11-10
    • 2021-08-29
    • 2011-03-17
    相关资源
    最近更新 更多