Sometimes, the unthinkable happens and an error occurs in your Magento system, causing everything to come grinding to a halt. Magento handles this by displaying a styled error page, which is better looking than a white page full of error text. However, the error page still doesn't look very professional, and potentially discloses information that could be abused by a malicious user. This post details a method for using a custom error page, and outlines some of the benefits of doing so.

Here is an example of the standard Magento error page:

The method we will use for a customised error page relies on the Apache web servers rewrite rules - if mod_rewrite is not enabled then this won't work (see our post on installing Magento for more information about mod_rewrite). The basic idea is to rewrite any URL in the report directory to display a specific error page which you define. This allows you to create a more friendly message for users who have been unfortunate enough to run into an error.

First, create a 503.html page in your document root (usually a public_html/ or www/ directory), or some other area the web server has access to, such as the Magento root directory. This should probably be a static HTML page rather than anything dynamic (i.e. PHP) to minimise the chance of the problem also preventing the error page from being served!

Next, add the following rule to the start of your Magento .htaccess file:

ErrorDocument 503 /path/to/file/503.html

Then, place this rule:

RewriteRule ^report/.* 503.html [L,R=503]

into the following code like so:

############################################
## you can put here your magento root folder
## path relative to web root

 #RewriteBase /magento/
 
############################################
## rewrite errors to 503 error document

 RewriteRule ^report/.* 503.html [L,R=503]

############################################
## workaround for HTTP authorization
## in CGI environment

This will then redirect any requests for an error report page using a 503 response code (Service Unavailable) and display the 503.html page in place of the standard Magento page. It should be noted that returning a 503 status code will also tell robots not to index the page content and to check again later, which means that your search engine rankings won't be affected.

The end result is that you can serve any visitor unlucky enough to encounter an error with something more friendly than the default error page, like this:

For developers that still need to access the error reports, they can still be found in the var/report directory off the Magento root - the filename will correspond with the id value in the error report URL requested. Alternatively, you can set up the system to email you whenever an error occurs by changing the report/config.xml file like so:

Change:

<action>print</action>

to

<action>email</action>

Now add in your email address to

<email_address></email_address>

By creating your own custom error page, you can have complete control over what visitors will see should a problem arise, rather than disclosing the internal details of your Magento installation and leaving the visitor with a less professional impression of the site.