Custom Magento error page
03 Sep 2009, by Chris Norton
4 Comments · Posted in Magento

    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.

    Comments

    Thanks for this. I have been searching around for an answer to the "less than professional" default error page. I haven't tried this yet, but really hope it works for me :)
    Comment by Anonymous - 4 Sep 2009 1:19:12 PM
    I guess you could also set up an IP mask to allow errors to come through to you, but nobody else, something like:

    Rewritecond %{REMOTE_HOST} !^1\.1\.1\.1
    Rewriterule ^report/.* 503.html [L,R=503]

    Where 1.1.1.1 is your IP, haven't tried it yet but this should be close.
    Comment by Ben - 10 Sep 2009 3:50:58 PM
    For those experiencing problems with this, you are probably using a version of Apache that is older than 2.2.x

    For those using a version before 2.2, use the following instead:


    ############################################
    ## rewrite errors to 503 error document

    RewriteRule ^report/.* /503.html [L]

    Basically, you need to drop the R flag. The Apache documentation states:

    "While this is typically used for redirects, any valid status code can be given here. If the status code is outside the redirect range (300-399), then the Substitution string is dropped and rewriting is stopped as if the L flag was used."

    I personally perceive this to be a bug - as it's inconsistent in that it does work on some 2.0 installs.

    Anyway - I hope this helps.
    Comment by Chris Owens - 6 Jan 2010 6:45:53 AM
    Hi,
    thx a lot for the great description.

    My errorpage is displayed now. Still the email-reporting isn't working yet.
    I changed the /report/config.xml as you described but I'm not getting any mail.

    Is there any specific setting in Magento to enable or set this mail-configuration?

    Thx & Regards
    Tom
    Comment by Tom - 31 Mar 2010 10:03:29 PM
    Comments are closed for this post