【问题标题】:Custom error page failed to load after editing htaccess编辑 htaccess 后自定义错误页面无法加载
【发布时间】:2016-07-03 16:05:03
【问题描述】:

我编辑了我的 htaccess 文件以将 mysite.com/page 重写为 mysite.com/page.php 并在用户请求 mysite.com/page.php 而不是 mysite.com/page 时显示 404 错误页面

我还添加了我自己的自定义 404 错误页面

但是当我请求mysite.com/page.php 时,显示的错误页面是 Apache 默认的 404 错误页面(而不是我的自定义错误页面),并带有额外的错误信息:

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

我的错误日志:

Request exceeded the limit of 10 internal redirects due to probable configuration error.

这是我的 htaccess 文件:

RewriteEngine On
RewriteBase /

RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]

RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]

ErrorDocument 404 /error/404

我已尝试将上面最后一行的 404 更改为 404.php,但自定义错误页面仍然没有显示..

但是,当我请求mysite.com/page/ 时,自定义错误页面确实出现了(注意斜线)。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:

    我不明白,这个错误是从哪里来的。但是您可以通过在请求404.php 时退出规则链来解决它。在开头插入这条规则

    RewriteRule ^error/404.php$ - [L]
    

    不相关,但您可以稍微简化一下规则

    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^ %{REQUEST_FILENAME}.php [L]
    
    RewriteCond %{THE_REQUEST} \.php
    RewriteRule \.php$ - [L,R=404]
    

    【讨论】:

    • 规则链退出解决方案效果很好。我现在也可以在请求中附加“/”,这样mysite.com/index/ 就不会抛出错误页面,并使 URL 更整洁
    【解决方案2】:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php
    ErrorDocument 404 /error/404.php
    

    【讨论】:

    • 它不起作用。使用 .php 请求时仍然没有错误
    • 您好,您能否为您的代码添加一些解释?这会在审核队列中弹出,因为只有代码的答案往往如此。
    猜你喜欢
    • 2015-07-16
    • 2019-03-13
    • 2017-06-11
    • 2020-05-01
    • 2017-10-18
    • 2021-10-26
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多