Custom Apache Error Handler – Get the Error URL in PHP

Failed URL for HTTP Error Code Redirect Pages

Scenario  

You are using an Apache and PHP environment.  You provide an ErrorDocument directive to create a common landing page for HTTP Errors (such as a custom 404 error page.)  This may have been setup either in your http.conf file or via an .htaccess file.  The rule might look like the following:

ErrorDocument 404 /errors/index.php

The issue is that you want to get details about what URL was accessed to cause the 404 or other http errors.  This post will show you how to do that.

Failed Attempts

First – some background.  In your searches you probably came across the $_SERVER variable which can be used to get details about the request being made.  The Document Root, the Script Page name, etc.  In this case you might have tried to use the server directive of “HTTP_REFERER”.  However you may have been frustrated to find this variable empty.  You may have also tried to use  “SCRIPT_FILENAME” however this only resulted in the filename of your custom script handler.  Let’s understand why these two variables did not work.

When Apache handles an error, the request is redirected to your custom error page.  It’s important to realize that this is treated as a new request.  This means that certain elements of the request itself has changed and will be different when it hits your custom error handler.

In the case of “HTTP_REFERER”, this variable is set by the User Agent (or browser).  So in this scenario, our request handler is actually a second request, performed by Apache, which means that the browser is not involved so this variable is never set. 

In the case of “SCRIPT_FILENAME”, this variable represents the file name of the currently running script.  So in this scenario, our error handler is actually a new request sent to our error handler page.  This variable in turn ends up being the file name of our error handling script.

Apache REDIRECT_ Variables

Don’t fret – Apache loves you.  Apache adds on several new variables to the error handler request.  Several of these start with the “REDIRECT_” string name.  Here are some examples.

REDIRECT_URL       The URL being redirected from.  In a 404 example, this would be the URL that was not found
REDIRECT_QUERY_STRING   Any query string data from the previous request.
REDIRECT_REQUEST_METHOD   The request method from the previous request.
REDIRECT_STATS   The HTTP error code that was generated, for example “404”

These variables above are added by Apache and can help you identify data from the previous request that resulted in the http error.

Additional Thoughts

You may notice that these variables do not show up in the PHP Documentation for the $_SERVER variable.  This is because they are not standard request variables.  These are added on when needed, in this case by Apache.

If you were to print out the entire array of data points for the $_SERVER variable, you might notice that there are several similar variables.  These include “QUERY_STRING” and “REQUEST_METHOD”.  Remember that these reference the current request (which is the error handler) and not the previous request that actually resulted in the HTTP error.

With this new data, when you handle a custom HTTP error, you can printout into your logs more detail about the page that actually caused the error.  Using the “REDIRECT_URL”, “REDIRECT_QUERY_STRING” and “REDIRECT_REQUEST_METHOD” you can often recreate the previous request that resulted in an error.  

 

Posted in , , , and tagged , , , .