An Informative 404 Page

by on 4th June 2008 with 15 Comments

Classic 404 error pages are prone to being relatively useless. Whilst a well designed page can provide a means to find what they are looking for, wouldn’t it be great if you could find out more about what went wrong? This tutorial will show you how simple it is to have an explanatory email sent to you whenever a visitor hits a 404 page.

Getting Started

The first thing is to implement a custom 404 error page. We’ve covered this before, but it essentially involves adding the following to your root .htaccess file:

ErrorDocument 404 http://www.yoursite.com/404.php

This will instruct all 404 error requests to be sent to the specified page rather than showing a default message.

The PHP Code

The following code needs to be customized and placed anywhere in your 404 php page:


This will send you an email which will include (if possible) both the page which the user has been referred from, and the page or resource which couldn’t be found.

Useful Applications

Implementing this has a whole host of useful outcomes. However, the first thing to note is that (unless you have previously added these files), a rapid number of emails will arrive in relation to:

  • favicon.ico
  • robots.txt

It may well be worth adding these files in before implementing the page so that you aren’t inundated with notifications! This technique can be invaluable when designing a new site, to sift out any broken links which may have been overlooked before launch. It is even more useful when modifying an existing site structure, to ensure that current links to your site are not taking new visitors to missing pages. If this is the case, simple redirects can be added using .htaccess:

For a file:

RewriteRule file1.php$ file2.php [L]

For a directory:

RewriteRule ^oldfolder/$  newfolder/ [L]

After a while, you’ll find that the odd 404 error is due to someone mistyping a URL, or having linked to a genuinely non-existent page on your site. It wouldn’t be advisable to keep this code in place permanently, but for a few days or weeks after making significant changes it provides a fantastic fail-safe.

Comments & Discussion

15 Comments

  • http://www.creativestable.com Amy

    I use Drupal and I have a neat module installed called “search404″. When a user enters a URL that does not exist, the site actually performs a search on the last bit of the URL and brings you to a page of results.

  • http://rotw.de/ Martin

    Ooh, 404. *reloads*. Ooh, again. *reloads*. Hey, perhaps I can annoy the admin! *reloads 10 times*. I can do that better. *uses a script to call the page 2000 times*.

    Not to mention the potential XSS issues. If you do a tutorial, please make sure the outcome is complete.

  • http://www. Virgil

    I can see the usefullness of knowing what errors people are getting but that should be handled by your statistic program. My inbox is full enough with spam and like Martin suggests there is the possibility of people expoiting it and overflowing your email, like they do not already have enough people expoiting contact forms etc. or email bots etc.

  • http://www.pixelhavenllc.com Josh Harbaugh

    I agree. Unless you’re dealing with a smaller site that wouldn’t generate as many 404s I would be looking to site statistics or analytics for page display errors.

  • http://www.erikreagan.com Erik Reagan

    I also agree. On any higher traffic site this would bombard someone with emails if a user just typed junk in for some reason. If you REALLY want to go a custom route (and not use a statistics or analytics program readily available) I would suggest writing these results to a file instead. If you want to get ‘fancy’ you can also create a cron job to mail the results weekly or however often you prefer.

  • http://www.designshack.co.uk David Appleyard

    You have all raised some very good points. This is mainly useful as a (very) temporary measure. When you launch a new site design or restructure it, it does allow you to find out the very common problematic pages easily. Another solution could be to write each 404 error to a database, then show a summary of the most popular errors.

  • http://www.imagelimited.co.uk Will

    I have them transferred to my inbox but i set a rule in outlook to automatically move them into my errors folder and i then sift through that folder weekly. Much better than receiving direct to inbox.

  • http://www.vebut.se Daniel Lindén

    Well, instead of emailing admin a database or log-file would be better. By using error-codes you can keep track of which messages that already has been submited and make it without spam. My first application was much like that, but later on I started to work with log-files which to me is the best solution, it even looks cool.

  • http://www.clikdevelopment.co.uk David Kindness

    Yep for anything other than a simple site this method is just going to fill your inbox up, not to mention the possibility of being spammed to death.

    As others suggested a combined approach of using your webstats and a log file would be more appropriate.

  • http://www.falcon-multimedia.com Jordan Payne

    The mailing idea isn’t such a good one. If some moron attempts to attack your site, they may find that vulnerability – flooding your email inbox.

    Instead i would advise using PHP’s error handling functions and write to an error file.

  • http://www.jobs.com angel

    I need more information

  • http://link Stinky82

    As is the case with medications. ,

  • http://www.dunkof.com iftar sahur

    thank you very good ;)

  • http://www.newyorkforum.us New York Forum

    We have a visitor that is always on our page 15 minutes per day but the logs do not show us what page so this code should embeded in our template should be able to tell exactly which pages they are on.

    Oh. We tested your 404 on your site befor reading Martin’s comment so sorry if that “dinner and a movie” flooded your inbox.

  • http://www.slimming-products.net slimming products

    I like it very much and which you shared the info in this post is very useful. Thanks for sharing a wonderful post. I found this post while surfing the web.Thanks for sharing this article.

Subscribe
Membership
About the Author