Apache ErrorDocument 不适用于 PHP 500 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2252624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache ErrorDocument not working for PHP 500 error
提问by
I have a number of ErrorDocumentssetup in my .htaccess file for errors such as 404, 401, 403 etc which all redirect to my error page but the ErrorDocumentset for a 500 error is never displayed when PHP reports a 500. The 500 code is sent to the browser and the output is blank. Is there something special I need to do to enable 500 error documents for use with PHP?
我的ErrorDocuments.htaccess 文件中有许多错误设置,例如 404、401、403 等,这些错误都重定向到我的错误页面,但是ErrorDocument当 PHP 报告 500 时,永远不会显示 500 错误的设置。500 代码被发送到浏览器和输出是空白的。我需要做一些特别的事情来启用 500 个错误文档以与 PHP 一起使用吗?
My directives look like this:
我的指令如下所示:
ErrorDocument 401 /errorpage.php?error=401
ErrorDocument 403 /errorpage.php?error=403
ErrorDocument 404 /errorpage.php?error=404
ErrorDocument 500 /errorpage.php?error=500
I've looked through the php.ini and can't see anything that would obviously override the Apache settings and there are no ErrorDocumentdirectives in my httpd.conf either. Anywhere else I should be looking?
我查看了 php.ini 并看不到任何明显会覆盖 Apache 设置的内容,并且ErrorDocument我的 httpd.conf 中也没有任何指令。我还应该去哪里找?
Thanks in advance.
提前致谢。
回答by Alexander Garden
回答by Bhumi Singhal
I happened to encounter the same issue while working with codeigniter and Imagick. imagick was setting a 500 HTTP error when something went wrong and in that case Codeigniter's custom 500 message was not displayed.
我在使用 codeigniter 和 Imagick 时碰巧遇到了同样的问题。当出现问题时,imagick 设置了 500 HTTP 错误,在这种情况下,Codeigniter 的自定义 500 消息未显示。
I resolved this by adding try-catch to all Imagick functions.Check that where from the 500 issue is arising and then add a try-catch there.As for
我通过向所有 Imagick 函数添加 try-catch 解决了这个问题。检查 500 问题是从哪里出现的,然后在那里添加一个 try-catch。至于
ErrorDocument 500 /errorpage.php?error=500
,I read loads online about the same where some people claim that this solved their similar issue, others say that it wouldnt help as Apache has handed over the control to PHP.Maybe some php code is setting Headers to 500 and that would probably lead to the browser displaying its custom 500 error message.
,我在网上阅读了大致相同的负载,有些人声称这解决了他们的类似问题,其他人说这无济于事,因为 Apache 已将控制权移交给 PHP。也许某些 php 代码将 Headers 设置为 500,这可能会导致浏览器显示其自定义 500 错误消息。
回答by Jake McGraw
You may need to add the ErrorDocument declaration earlier in the Apache conf chain. If you add this to a vhost conf it may not be called.
您可能需要更早地在 Apache conf 链中添加 ErrorDocument 声明。如果您将此添加到 vhost conf,则可能不会调用它。
回答by mattbasta
I don't believe Apache will let you run PHP files for 500 errors because the error page could generate an error. Try rendering out your 500 error to an HTML file and point your directives at that.
我不相信 Apache 会让你运行 PHP 文件来处理 500 个错误,因为错误页面可能会产生错误。尝试将您的 500 错误呈现到 HTML 文件中,并将您的指令指向该文件。

