An Informative 404 Page
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.
Get unlimited downloads of 2 million+ design resources, themes, templates, photos, graphics and more. Envato Elements starts at $16 per month, and is the best creative subscription we've ever seen.
Shopify, Tumblr & More
Landing Pages & Email
Icons, Vectors & More
Logos, Print & Mockups
Sans Serif, Script & More
PowerPoint & Keynote
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:
$myemail = "insert your email address here";
$page = 'http://' . $SERVER_NAME . $REQUEST_URI;
$subject = "404 Page Error";
$message = "
404 Page Error Report
Visitor came from: ".$_SERVER['HTTP_REFERER']."
Page not found: ".$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.
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:
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.