One of the first things you learn to do in web devel­op­ment is make sure every­thing works. Do all of your links work cor­rectly? Do all of your images show up? And as your web­site pro­gresses and grows, is some­one link­ing to one of my pages that doesn’t exist any­more? Or are peo­ple look­ing for a page they think should exist but doesn’t? You can check on all of that with some basic test­ing and look­ing through bor­ing server logs, or you could make your web­site work for you and tell you when some­one couldn’t find something.

In Word­Press, when a page or file can’t be found a user is redi­rected to the 404.php page. Mine looks like this:

404 Landing Page for MaxBeatty.com

404 Land­ing Page for MaxBeatty.com

I came across a great tuto­r­ial from Net­tuts+ for cre­at­ing a PHP auto-mailer for 404 and 403 errors, but since Word­Press already had it’s own 404.php page, I decided to sim­plify their method a bit.

At the top of my Word­Press theme’s 404.php page, I added some sim­ple PHP to get envi­ron­men­tal vari­ables, con­struct an email, then mail it to myself before load­ing the rest of the page.

Replace:[php]<?php include_once ‘header.php’;?>[/php]

With:[php]<?php
# Gather vis­i­tor infor­ma­tion
$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 Ref­erer
$http_agent = getenv("HTTP_USER_AGENT");    // User Agent
$error_date = date("D M j Y g:i:s a T");     // Error Date
$msg­body =
"There was a 404 error on the ".$server_name." domain".
"nnDetailsn———————————————————————-".
"nWhen: ".$error_date.
"n(Who) IP Address: ".$ip.
"n(What) Tried to Access: http://".$server_name.$request_uri.
"n(From where) HTTP Ref­erer: ".$http_ref.
"nnUser Agent: ".$http_agent;
mail("you@yourdomain.com", "maxbeatty.com 404 report", $msg­body, "From: 404@yourdomain.comn");

include_once ‘header.php’;
?>[/php]

Now every time there is a 404 error in Word­Press, you’ll get an email about it. But wait, do you really want your inbox clut­tered with error emails you may not be able to address until the week­end? No– enter Gmail fil­ters.

In Gmail, go to Set­tings then click on Fil­ters. 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 tech­ni­cal details will look spammy.

Filter messages from '404@yourdomain.com'

Fil­ter mes­sages 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 pro­duced for the day/week/month, they’ll all be wait­ing for your review under the ‘404 Errors’ label in Gmail. From there you can try to recre­ate and diag­nose the problems.

If you’re lazy or scared of edit­ing code, there are Word­Press plu­g­ins our there to do all of this for you and even enhance your bor­ing 404 land­ing page.

  • 404 Noti­fier is a plu­gin that will send you emails any­time a 404 error is pro­duced. You might still want to set up a Gmail fil­ter for their emails.
  • 404 SEO Plu­gin will “auto­mat­i­cally dis­play links to rel­e­vant pages on your site, based on the words in the URL that was not found.”
  • Smart 404 will “per­form a search of your posts, pages, tags and cat­e­gories, using key­words from the requested URL. If there’s a match, redi­rect to that con­tent instead of show­ing the error. If there’s more than one match, the 404 tem­plate can use some tem­plate tags to pro­vide a list of sug­ges­tions to the vis­i­tor.” I think this could be annoy­ing if a user KNOWS what they are look­ing for, but could be help­ful 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 look­ing for!