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

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

问题描述

我的 .htaccess 文件中有一堆 ErrorDocument 指令,以便捕获 Apache 可以向用户抛出的几乎所有可能的错误,并将所述用户重定向到我的错误控制器,然后将错误呈现给更多用户友好的方式.但是,它似乎不起作用.

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.

例如,当我输入无效的 URL 时,例如 mysite.com/""##$##$!我总是收到 Apache 的默认 403 错误消息,而不是重定向到我的 errors.php 文件.下面是我正在使用的指令.我需要做任何其他事情才能使它工作吗?(AllowOverride All 已设置)

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.

谢谢!

哦,只是想我会提到这个.我编写了自己的 MVC 结构来重定向请求,并且运行良好.在 PHP 中,如果用户请求一个不存在的 URL,我自己的 404 错误将很好地触发(或我定义的任何其他错误).所以基本上,如果我输入 mysite.com/!!!!进入 URL,它将起作用,我得到 404.但是,每当我用双引号字符开始请求时,都会触发默认的 Apache 403 错误.奇怪的.此外,500 错误也将无法重定向到 errors.php,并且只会返回默认的 Apache 500 屏幕(例如,在使用 header("HTTP/1.0 500 Internal Server Error");die(); 进行测试时).

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();).

编辑 3:我只是尝试将 ErrorDocument 200 "message" 放在我的 .htaccess 文件中,但没有任何反应,我打开的任何页面都可以正常打开.最坏的情况是,这应该陷入无限循环.最好的情况,它应该输出消息".它两者都没有,它只是忽略了 ErrorDocument.Apache 的访问日志记录了一个正常的 200 OK 标头,但它被 .htaccess 完全忽略了.

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.

推荐答案

问题中有几个不同的误解.以下 PHP 代码:

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

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

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

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 仅适用于错误代码,不适用于成功代码.它在文档中

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.

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

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:

虽然大多数错误信息都可以覆盖,有一定的内部的情况消息被使用而不管ErrorDocument 的设置.在特别是,如果格式错误的请求是检测到,正常请求处理将立即停止并返回内部错误消息.这个有必要防范安全由错误请求引起的问题.

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.

<小时>

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


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

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

And then adding a perl script that crashes:

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

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

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

但是,您在使用 PHP 时不太可能触发 Apache 500,因此这可能不是一个有用的测试.

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天全站免登陆