One of the first things you learn to do in web development is make sure everything works. Do all of your links work correctly? Do all of your images show up? And as your website progresses and grows, is someone linking to one of my pages that doesn’t exist anymore? Or are people looking for a page they think should exist but doesn’t? You can check on all of that with some basic testing and looking through boring server logs, or you could make your website work for you and tell you when someone couldn’t find something.

In WordPress, when a page or file can’t be found a user is redirected to the 404.php page. Mine looks like this:

404 Landing Page for MaxBeatty.com

404 Landing Page for MaxBeatty.com

I came across a great tutorial from Nettuts+ for creating a PHP auto-mailer for 404 and 403 errors, but since WordPress already had it’s own 404.php page, I decided to simplify their method a bit.

At the top of my WordPress theme’s 404.php page, I added some simple PHP to get environmental variables, construct an email, then mail it to myself before loading the rest of the page.

Replace:

<?php include_once 'header.php';?>

With:

<?php
# Gather visitor information
$ip = getenv("REMOTE_ADDR");                // IP Address
$server_name = getenv("SERVER_NAME");       // Server Name
$request_uri = getenv("REQUEST_URI");       // Requested URI
$http_ref = getenv("HTTP_REFERER");         // HTTP Referer
$http_agent = getenv("HTTP_USER_AGENT");    // User Agent
$error_date = date("D M j Y g:i:s a T");     // Error Date
$msgbody =
"There was a 404 error on the ".$server_name." domain".
"\n\nDetails\n----------------------------------------------------------------------".
"\nWhen: ".$error_date.
"\n(Who) IP Address: ".$ip.
"\n(What) Tried to Access: http://".$server_name.$request_uri.
"\n(From where) HTTP Referer: ".$http_ref.
"\n\nUser Agent: ".$http_agent;
mail("you@yourdomain.com", "maxbeatty.com 404 report", $msgbody, "From: 404@yourdomain.com\n");
include_once 'header.php';
?>

Now every time there is a 404 error in WordPress, you’ll get an email about it. But wait, do you really want your inbox cluttered with error emails you may not be able to address until the weekend? No- enter Gmail filters.

In Gmail, go to Settings then click on Filters. You want to label any emails from ’404@yourdomain.com’ as ’404 Errors’, tell them to skip the inbox, and never be marked spam because odds are the URLs and technical details will look spammy.

Filter messages from '404@yourdomain.com'

Filter messages from '404@yourdomain.com'

Skip the inbox, apply the label, and never mark as spam

Skip the inbox, apply the label, and never mark as spam

Then when you have time to look over errors that your site has produced for the day/week/month, they’ll all be waiting for your review under the ’404 Errors’ label in Gmail. From there you can try to recreate and diagnose the problems.

If you’re lazy or scared of editing code, there are WordPress plugins our there to do all of this for you and even enhance your boring 404 landing page.

  • 404 Notifier is a plugin that will send you emails anytime a 404 error is produced. You might still want to set up a Gmail filter for their emails.
  • 404 SEO Plugin will “automatically display links to relevant pages on your site, based on the words in the URL that was not found.”
  • Smart 404 will “perform a search of your posts, pages, tags and categories, using keywords from the requested URL. If there’s a match, redirect to that content instead of showing the error. If there’s more than one match, the 404 template can use some template tags to provide a list of suggestions to the visitor.” I think this could be annoying if a user KNOWS what they are looking for, but could be helpful if a user has a typo in the URL.

Hope this quick copy/paste will improve your site and help your users find what they’re looking for!