views:

90

answers:

5

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?

A: 

Check your webserver's access log. There you will be able to see which request that is causing the 500 response code.

In Apache (using the default log format), a successful request (200 OK) could look like this:

127.0.0.1 - - [19/Jul/2010:18:25:54 +0200] "GET / HTTP/1.1" 200 663 "-" "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.8pre) Gecko/20100718 Ubuntu/10.04 (lucid) Namoroka/3.6.8pre"

A request that results in a 500 could look like this:

127.0.0.1 - - [19/Jul/2010:18:24:37 +0200] "GET / HTTP/1.1" 500 631 "-" "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.8pre) Gecko/20100718 Ubuntu/10.04 (lucid) Namoroka/3.6.8pre"

The response code is in the sixth column.

You can also check your PHP error log, which will contain all PHP errors assuming you've enabled error logging (you should).

Daniel Egeberg
I don't want to detect it after the fact though, I want PHP to detect it right there and do my own processing with it.
animuson
+3  A: 

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.

Charles
+1  A: 

You could always look in the referrer field of the http request to determine on which URL the caller was before

Gab Royer
A: 

Your apache server logs every Error occurs(Though it is configurable). You can able to open and manage this file using FileStream in your PHP website. See http://httpd.apache.org/docs/2.2/logs.html to see more about Apache log file. You can get a tutorial on .htaccess from http://www.freewebmasterhelp.com/tutorials/htaccess/.

To make your own processing, make a PHP file like error.php which will process your errors. You can redirect it using .htaccess file. After that if you want to go to a page you can do that by using header() method.

chanchal1987
A: 
ErrorDocument 500 http://www.example.com/500.php

in the 500.php check

$_SERVER['HTTP_REFERER']

it should be the URL of page that has internal server error. for more information (if you want) you should check the log.

Ehsan