Search code examples
php.htaccesshttp-redirecthttp-referer

How do you detect what page caused the Internal Server Error?


So, in my .htaccess file I have this ErrorDocument lines:

ErrorDocument 500 http://www.example.com/500

Since my server runs multiple websites from the same core files, I just want to redirect all internal server errors to the same processing page. However, my problem is that it doesn't send any information about the page that cause the error, it redirects the page. I tried changing it to ErrorDocument 500 index.php?500 but that just causes a second internal server error when trying to locate the file. Any ideas on how I can successfully redirect it to my custom 500 error page and still acquire information about the page that caused the error in the first place?


Solution

  • In your ErrorDocument statement, you're giving a URL to a remote page. As a result, Apache sends the user a Location header, and the user goes off on their merry way.

    Instead, change the URL to an absolute path to a local script that will handle the error:

    ErrorDocument 500 /500.php
    

    The script should be launched with a set of environment variables starting with REDIRECT_ that should contain the various paths and query strings involved in the error.

    There is no way to both send the user elsewhere and also capture the information within ErrorDocument itself. On the other hand, your script can capture the information and then redirect the user, if you still want to handle it that way for some reason.