Apache的ErrorDocument指令不重定向 [英] Apache's ErrorDocument directive does not redirect

查看:192
本文介绍了Apache的ErrorDocument指令不重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.htaccess文件中有一堆ErrorDocument指令,以捕捉到Apache可能在用户身上丢失的几乎所有可能的错误,并将所有用户重定向到我的错误控制器,然后这个错误控制器会在更多用户中呈现错误友好的方式。但是,它似乎不起作用。



例如,当我输入一个无效的URL,如mysite.com/\"\"##$##$!我总是得到Apache的默认403错误消息,而不是重定向到我的errors.php文件。以下是我使用的指令。我需要做任何其他事情来使其发挥作用吗? (AllowOverride全部设置)

  ErrorDocument 403 /errors.php 

如果这是绝对自定义错误处理的错误方法,请让我知道,我会感谢向正确的方向微调。



谢谢!



编辑:
哦,只是想我会提到这个。我编写了自己的MVC结构来重定向请求,并且它的效果很好。从PHP内部,如果用户请求不必要的URL,我自己的404错误将会正常启动(或者我已经定义的任何其他错误)。所以基本上,如果我进入mysite.com / !!!!进入URL,它将工作,我得到一个404.但是,每当我使用双引号字符启动请求时,默认的Apache 403错误将触发。奇。此外,500错误将无法重定向到errors.php,并将简单地返回一个默认的Apache 500屏幕(例如,当使用标头测试(HTTP / 1.0 500内部服务器错误); die();) 。



编辑3:我刚刚尝试在.htaccess文件中放置 ErrorDocument 200message我打开的任何页面都正常打开。最糟糕的情况是,这应该会陷入无限循环。最好的情况,应该输出消息。它没有,它只是忽略ErrorDocument。 Apache的访问日志注意到一个正常的200 OK标题,但它完全被.htaccess忽略。

解决方案

这个问题的概念。以下PHP代码:

 标题(HTTP / 1.0 500内部服务器错误); 
die();

永远不会触发Apache错误页面 - 它触发浏览器的默认错误页面。一旦将控制权转交给PHP,它就不会回到Apache进行错误处理。



ErrorDocument仅适用于错误代码,而不是成功代码。它在文档中

 语法:ErrorDocument错误代码文档

http:/ /httpd.apache.org/docs/current/mod/core.html#errordocument



如果您误认为某种服务器的浏览器错误页面错误,那可能是您的主要问题的原因。除非您的自定义错误处理程序输出一定量的数据,否则一些浏览器将始终显示自己的错误页面。确保您的输出大小至少为几千字节。



您的问题的原因很可能就是Apache的内置行为以及您选择的测试URL。来自ErrorDocument文档:


尽管大多数错误消息可能是
被覆盖,但是有一些
的情况是内部的
消息被使用,不管
设置的ErrorDocument。在
特定的情况下,如果检测到错误的请求是
,则正常请求处理
将立即停止,并返回
内部错误消息。这个
是必要的,以防止由不良请求引起的安全性
问题。







编辑:如何在Apache中模拟500错误。我的第一个想法是 .htaccess 中的语法错误,但这不会触发自定义错误处理程序。我找到的最简单的方法是通过添加以下行,在 .htaccess 中启用CGI:

  ErrorDocument 500 /500.php 
选项+ ExecCGI
AddHandler cgi-script .pl

然后添加一个崩溃的Perl脚本:

 #!/ usr / bin / perl 
safasfdsfdd _(* EYFIUOBAF(_ * AS ^ FD _(* AWHD {

您需要确保Perl脚本可以由apache的用户执行,这显示了我的自定义500处理程序。



但是,使用PHP时,您不太可能会触发Apache 500,所以这可能不是有用的测试。


I have a bunch of ErrorDocument directives in my .htaccess file in order to catch almost all the possible errors Apache can throw at a user, and to redirect said user to my error controller which would then render the error in a more user friendly manner. However, it does not seem work.

For instance, when I enter an invalid URL like mysite.com/""##$##$! I always get Apache's default 403 error message, instead of a redirect to my errors.php file. Below is the directive I'm using. Do I need to do anything else to make it work? (AllowOverride All is set)

   ErrorDocument 403 /errors.php

If this is the wrong way to approach absolute custom error handling, please let me know, I would appreciate a nudge in the right direction.

Thank you!

Edit: Oh, just thought I'd mention this. I wrote my own MVC structure for redirecting the request, and it works well. From within PHP, if a user requests a nonexistant URL, my own 404 error will fire just fine (or whatever other error I have defined). So basically, if I enter mysite.com/!!!! into the URL, it will work and I get a 404. However, whenever I start a request with the double quote character, the default Apache 403 error fires. Odd. Also, a 500 error will fail to redirect to errors.php as well, and will simply return a default Apache 500 screen (for instance, when testing with header("HTTP/1.0 500 Internal Server Error");die();).

Edit 3: I just tried placing ErrorDocument 200 "message" in my .htaccess file and nothing happened, any page I open opens normally. Worst case scenario, this should get stuck in an infinite loop. Best case scenario, it should output "message". It did neither, it simply ignored ErrorDocument. Apache's Access Log notes a normal 200 OK header, and yet it was completely ignored by .htaccess.

解决方案

A few different mis-conceptions in the question. The following PHP code:

header("HTTP/1.0 500 Internal Server Error");
die();

Will never trigger an Apache error page - it's triggering your browser's default error page. Once control has been given over to PHP, it does not go back to Apache for error handling.

ErrorDocument only works for error codes, not success codes. It's in the docs

Syntax: ErrorDocument error-code document

http://httpd.apache.org/docs/current/mod/core.html#errordocument

If you were mistaking one kind of browser error page for a server error, then that might be the cause of your main problem too. Unless your custom error handler outputs a certain amount of data, some browsers will always show their own error pages. Make sure your output is at least a few kilobytes in size.

The cause of your problem is most likely just Apache's built-in behavior combined with your choice of test URLs. From the ErrorDocument docs:

Although most error messages can be overriden, there are certain circumstances where the internal messages are used regardless of the setting of ErrorDocument. In particular, if a malformed request is detected, normal request processing will be immediately halted and the internal error message returned. This is necessary to guard against security problems caused by bad requests.


Edit: How to simulate a 500 error in Apache. My first thought was syntax errors in .htaccess, but this wont trigger custom error handlers. The easiest way I found was to enable CGI in .htaccess by adding these lines:

ErrorDocument 500 /500.php
Options +ExecCGI
AddHandler cgi-script .pl

And then adding a perl script that crashes:

#!/usr/bin/perl
safasfdsfdd_(*EYFIUOBAF(_*AS^FD_(*AWHD{

You will need to make sure the perl script is executable by apache's user. This shows my custom 500 handler.

However, you're very unlikely to ever trigger an Apache 500 when using PHP, so this probably isn't a useful test.

这篇关于Apache的ErrorDocument指令不重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆