Apache's ErrorDocument directive does not redirect Apache's ErrorDocument directive does not redirect php php

Apache's ErrorDocument directive does not redirect


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.phpOptions +ExecCGIAddHandler cgi-script .pl

And then adding a perl script that crashes:

#!/usr/bin/perlsafasfdsfdd_(*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.


I just use .htaccess for all custom error pages

ErrorDocument 401 /error/PHP/server-error.php?error=401ErrorDocument 403 /error/PHP/server-error.php?error=403ErrorDocument 404 /error/PHP/server-error.php?error=404ErrorDocument 500 /error/PHP/server-error.php?error=500ErrorDocument 503 /error/PHP/server-error.php?error=503