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

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

问题描述

我有一堆在我的.htaccess文件的ErrorDocument指令以赶上几乎所有可能的错误Apache可以在用户throw和重定向用户说我的错误控制器随后这会使错误更用户友好的方式。然而,这似乎并没有工作。

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.

例如,当我输入的网址无效像mysite.com/\"\"##$##$!我总是得到Apache默认的403错误消息,而不是重定向到我的errors.php文件。下面是我使用的指令。我需要做别的事,使其工作? (所有的AllowOverride设置)

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

如果这是接近绝对的自定义错误处理方式不对,请让我知道,我会AP preciate在正确的方向轻推。

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屏幕(比如,用头测试(HTTP / 1.0 500内部服务器错误)时;模具();)

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消息在我的.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 code:

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只对错误codeS,不是成功codeS。它在文档

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

Syntax: ErrorDocument error-code document

<一个href=\"http://httpd.apache.org/docs/current/mod/core.html#errordocument\">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的内置行为与您所选择的测试网址相结合。从文档的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{

您将需要确保perl脚本是Apache的用户执行。这说明我的自定义500处理器。

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

不过,你是不太可能永远触发一个Apache 500使用PHP的时候,所以这可能不是一个有用的测试。

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