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:

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:

For a directory:

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.

For more related resources: