6 Steps to a Better 404 Page

We’ve all expe­ri­enced it. We’re read­ing a Web page only to come across a link which seems inter­est­ing enough to fol­low. So we do, and it happens:

404 Page Not Found

The URL you requested was not found.

Apache 2 Web server blah blah blah

Not a very help­ful error page, is it? What does “404” even mean to the aver­age Web surfer? Sadly, the Web is lit­tered with 404 error pages sim­i­lar to what is quoted above. And what’s wrong with such error pages?

The Problem

  • They aren’t very friendly. Your vis­i­tors are human beings — talk to them like it. Treat them as your guests; they’ll be more likely to for­give the error and con­tinue to browse your site!

  • They con­tain super­flu­ous infor­ma­tion. Seriously, why would Joe the Visitor care what server your site is hosted on unless he is a mali­cious user look­ing to exploit it? Why would Jane the Visitor care about “404” or other tech­no­jar­gon? I don’t even care about that sort of infor­ma­tion, and I’m just geeky enough to know what it means!

  • They con­tain miss­ing infor­ma­tion. This may be a novel con­cept, but rather than sim­ply stat­ing that an error occurs, why not go out of your way to help your user get to some­thing they’d enjoy view­ing? No, I don’t mean auto­mat­i­cally redi­rect­ing them to your cur­rent YouTube favorite. What I do mean is shar­ing with your users any of a num­ber of pos­si­bly help­ful items: a list of your pop­u­lar con­tent, a list of pop­u­lar top­ics to browse, a freakin’ search box, or even a list of sug­gested con­tent based off of the errant address which caused the 404.

The Solution

Your con­tent not found page doesn’t have to be that way, though. It can be bet­ter — lots bet­ter! The fol­low­ing six steps are “best prac­tices” in my opin­ion. I am using them here to cre­ate an error page which Chris Pearson says is “WIN for most help­ful 404″ (Twitter).

Your mileage may, of course, vary, and I encour­age you to tai­lor each step to the theme of your blog (i.e., if you have a funny blog, make your error page funny!).

  1. Explanation: Explain to your users what has hap­pened, why they are view­ing an error page. Reassure your users that it is not their fault, and invite them to make use of the help­ful resources you’re pro­vid­ing on the error page to get back on track. The faster they move from your error page to some­thing more inter­est­ing, the less time the error page will have to affect the men­tal­ity of your vis­i­tor. “Oh yeah, I remem­ber that site… 404s-​​ville.”

  2. Search: Always, always, always pro­vide search func­tion­al­ity on your con­tent not found errors. Let me repeat: Always pro­vide search func­tion­al­ity on your con­tent not found errors. You may have removed the arti­cle on “foo” that you users browsed to, but if they are able to search for “foo” on the error page, they may find a num­ber of other help­ful arti­cles you’ve writ­ten on the sub­ject. (This ties in to the thought that rep­e­ti­tion is the best teacher and any­thing impor­tant on your blog prob­a­bly bears blog­ging more than once.) It doesn’t mat­ter if you already have a search bar in your side­bar; put one right in front of your user’s nose in the con­tent of the error page itself. Don’t risk “side­bar blind­ness” ren­der­ing your error page less than useful.

  3. Popular con­tent: Whether you’re track­ing it or not, your blog has entries which is more pop­u­lar than oth­ers. There is a rea­son for this: It it’s pop­u­lar, your users like it! It only makes sense, then, that if your users get off the beaten path on your web­site, you would show them a list of pop­u­lar con­tent. If oth­ers have enjoyed it, why not them, right?

    In order to track pop­u­lar con­tent here, I make use of the Popularity Contest plu­gin for WordPress (Installing Popularity Contest on WordPress 2.5+). It allows me to use this block of code on my con­tent not found page to out­put the crème de la crème of Kingdom Geek:

    <h3>Popular content:</h3>
    <ul>
    <?php akpc_most_popular(); ?>
    </ul>

    Whatever method you use to list pop­u­lar posts (by vis­its, by com­ments, or by both via Popularity Contest), I highly rec­om­mend that you at least do some­thing. Your users will appre­ci­ate it.

  4. Popular top­ics: There’s a chance that none of your most pop­u­lar posts will tickle your users’ fancy, and that’s per­fectly fine. Thankfully, for the past sev­eral ver­sions, WordPress has allowed us to tag our con­tent. You do tag, don’t you? Tagging our posts allows us to cre­ate a top­i­cal index of our con­tent which can be far more use­ful than the basic cat­e­gor­i­cal index. It also allows us to out­put a list­ing — in famous “tag cloud” form — of our most writ­ten about top­ics. The arti­cle on “foo” that your user was look­ing for may be miss­ing, but they may find just what they need in your arti­cles about “fubar.” Using this bit of code, you can add pop­u­lar top­ics to your error page:

    <h3>Popular topics:</h3>
    <p><?php wp_tag_cloud(); ?></p>

    You can fur­ther cus­tomize your tag cloud using the attrib­utes listed in the WordPress codex!<?p>

  5. Google 404 wid­get: It’s hard to deny how use­ful Google’s wiz­ardy is. From rev­o­lu­tion­iz­ing search to unleash­ing a legion of use­ful prod­ucts such as Gmail, Reader, or Maps, Google is the one per­son in your life who you can exploit with­out fear of face-​​slaps!

    Recently, Google unveiled a new tool for web­mas­ters to help increase Big G’s expo­sure web­mas­ters make their sites more use­ful: the Enhance 404 wid­get.

    Basically, this new tool allows web­mas­ters who have added their site to Google’s Webmaster Tools pro­gram — suave web­mas­ters like you & I — to add a snip­pet of JavaScript to our con­tent not found pages which allows Google to offer up some poten­tially use­ful tools. The tools which are out­put depends upon how much Google knows about your site, but the include things such as a Google search box, “clos­est match” sug­ges­tions, and a link to your site’s sitemap, if you have one.

    That’s pretty use­ful, in my opin­ion, and it’s very easy to add. Google gives you a piece of code to add to your error page, you add the code, and it just works! Mine looks like this:

    <script type="text/javascript">
    <!--//--><![CDATA[//><!--
    	var GOOG_FIXURL_LANG = 'en';
    	var GOOG_FIXURL_SITE = 'http://rickbeckman.com/';
    //--><!]]>
    </script>
    <script type="text/javascript" src="http://linkhelp.clients.google.com/tbproxy/lh/wm/fixurl.js"></script>

    The fancy JavaScript escap­ing is a method I learned from Jeff at Perishable Press.

    You may also notice that I omit­ted Google’s styling infor­ma­tion. I moved it to my stylesheet and cus­tomized it to look nice within my style:

    #goog-wm h3 b { font-weight: normal; }
    #goog-wm-qt { width: 68%; margin-right: 0.462em; }
    #goog-wm-sb { color: #111; cursor: pointer; font-weight: bold; background: url('../images/submit-bg.gif'); border-color: #ccc #999 #999 #ccc !important; border: double 3px; font-size: 1.2em; padding:0.278em 0.222em; font-family: "Gill Sans MT", "Gill Sans", Calibri, "Trebuchet MS", sans-serif; }
    	#goog-wm-sb:hover { color: #1e39cc; }
  6. Notification: I men­tioned ear­lier that the blame for a con­tent not found error should not be placed on the user. A corol­lary to that would be know­ing that the onus to fix the prob­lem is upon you. As a web­mas­ter, you should be will­ing to track down the rea­son con­tent not found errors are hap­pen­ing, rec­ti­fy­ing the problem(s) as soon as possible.

    If the prob­lem is that the user has fol­lowed a bad link, then you’re going to want to get that link fixed. To facil­i­tate this, I rec­om­mend adding a tiny bit of PHP code to auto­mat­i­cally notify the admin e-​​mail address of your blog of the con­tent not found error. This noti­fi­ca­tion is com­pletely anony­mous for your users; only the bro­ken address and the refer­ral address (if there is one) are listed in the email.

    I also have the code set to ignore blog admin­is­tra­tors so that you can test your error page with­out get­ting inun­dated with “Get this fixed!” e-​​mail messages.

    Here is the block of code to han­dle the notification:

    if (!current_user_can('level_10')) {
    	$referral		= $_SERVER['HTTP_REFERER'];
    	$not_found		= $_SERVER['SCRIPT_URI'];
    	$to				= get_option('admin_email');
    	$subject		= 'Content Not Found @ ' . get_option('blogname');
    	$content		= '';
    	if ($_SERVER['HTTP_REFERER'])
    		$content	= "Came from: $referral\n";
    	$content		.= "Landed on: $not_found\n\nGet this fixed!";
    	$headers		= 'From: ' . get_option('admin_email');
    
    	mail($to, $subject, $content, $headers);
    }

That’s a lot to take in, I know, but I encour­age you to apply as much of this as pos­si­ble to your error page, doing your part to make even the dark, erro­neous under­side of the Web that much more use­ful. If your users see that you took time to even make your error page use­ful, it will def­i­nitely set you apart from most other sites out there.

All Together Now

It may help to see the above used within the con­text of an actual error page, so I’m going to share the code I use to gen­er­ate the con­tent not found page in use here, video and all:

For the Thesis Theme

If you use the amaz­ing Thesis theme, you’ll want to add the fol­low­ing block of code to your custom/custom_functions.php file. Don’t for­get to cus­tomize it to fit your own site!

/**
*
*	404 Customizations
*
*/

function custom_404_title() {
	echo 'Content Not Found';
}
remove_action('thesis_hook_404_title', 'thesis_404_title');
add_action('thesis_hook_404_title', 'custom_404_title');

function custom_404_content() {
	if (!current_user_can('level_10')) {
		$referral		= $_SERVER['HTTP_REFERER'];
		$not_found		= $_SERVER['SCRIPT_URI'];
		$to				= get_option('admin_email');
		$subject		= 'Content Not Found @ ' . get_option('blogname');
		$content		= '';
		if ($_SERVER['HTTP_REFERER'])
			$content	= "Came from: $referral\n";
		$content		.= "Landed on: $not_found\n\nGet this fixed!";
		$headers		= 'From: ' . get_option('admin_email');

		mail($to, $subject, $content, $headers);
	} ?>
<p><span class="drop_cap">U</span>nfortunately, the content you are looking for could not be found. There are a variety of reasons why this could have occurred, but what is important is getting you back on track and hopefully to content that you will find to be useful.</p>
<div class="video">
	<object width="425" height="349">
		<param name="movie" value="http://www.youtube.com/v/yGZdqLgKrLE&hl=en&fs=1&rel=0&color1=0x2b405b&color2=0x6b8ab6&border=1"></param>
		<param name="allowFullScreen" value="true"></param>
		<param name="allowscriptaccess" value="always"></param>
		<embed src="http://www.youtube.com/v/yGZdqLgKrLE&hl=en&fs=1&rel=0&color1=0x2b405b&color2=0x6b8ab6&border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="349"></embed>
	</object>
</div>
<p>In order to help you find what you need, you might consider using the search bar or browsing my categories in the sidebar. If you'd prefer to browse popular content or topics, they are listed below.</p>
<h3>Popular content:</h3>
<ul>
<?php akpc_most_popular(); ?>
</ul>
<h3>Popular topics:</h3>
<p><?php wp_tag_cloud(); ?></p>
<script type="text/javascript">
<!--//--><![CDATA[//><!--
	var GOOG_FIXURL_LANG = 'en';
	var GOOG_FIXURL_SITE = 'http://rickbeckman.com/';
//--><!]]>
</script>
<script type="text/javascript" src="http://linkhelp.clients.google.com/tbproxy/lh/wm/fixurl.js"></script>
<p>I hope you find what you are looking for!</p>
<p><?php if ($_SERVER['HTTP_REFERER']) { ?>I was automatically notified of the address of this page as well as the referrer address which contained the errant link, if there was one, so that I can see about fixing broken links. However, y<?php } else echo 'Y'; ?>ou are more than welcome to e-mail me regarding this error; I would love to hear from you, especially if you still need help finding something! My e-mail address is <a href="http://mailhide.recaptcha.net/d?k=01b-IllrVLyH5ns4ZAetQxxw==&amp;c=8-jNc3fhiknDjwtt2Xmz3g8AQdbKMET295VYd1WAoa0=" onclick="window.open('http://mailhide.recaptcha.net/d?k=01b-IllrVLyH5ns4ZAetQxxw==&amp;c=8-jNc3fhiknDjwtt2Xmz3g8AQdbKMET295VYd1WAoa0=', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300'); return false;" title="Reveal this e-mail address">rick...</a>@gmail.com.</p>
<?php }
remove_action('thesis_hook_404_content', 'thesis_404_content');
add_action('thesis_hook_404_content', 'custom_404_content');

For Most Other WordPress Themes

Your theme should pro­vide you with a 404.php file with which you can cre­ate your error page. If it does, open it and add the fol­low­ing block of code in the “con­tent” sec­tion of the page; if you need help deter­min­ing where that is, it may help get­ting in touch with the theme author or, if that isn’t prac­ti­cal, get in touch with me and I’ll see what I can do for you.

<?php	if (!current_user_can('level_10')) {
		$referral		= $_SERVER['HTTP_REFERER'];
		$not_found		= $_SERVER['SCRIPT_URI'];
		$to				= get_option('admin_email');
		$subject		= 'Content Not Found @ ' . get_option('blogname');
		$content		= '';
		if ($_SERVER['HTTP_REFERER'])
			$content	= "Came from: $referral\n";
		$content		.= "Landed on: $not_found\n\nGet this fixed!";
		$headers		= 'From: ' . get_option('admin_email');

		mail($to, $subject, $content, $headers);
	} ?>
<p><span class="drop_cap">U</span>nfortunately, the content you are looking for could not be found. There are a variety of reasons why this could have occurred, but what is important is getting you back on track and hopefully to content that you will find to be useful.</p>
<div class="video">
	<object width="425" height="349">
		<param name="movie" value="http://www.youtube.com/v/yGZdqLgKrLE&hl=en&fs=1&rel=0&color1=0x2b405b&color2=0x6b8ab6&border=1"></param>
		<param name="allowFullScreen" value="true"></param>
		<param name="allowscriptaccess" value="always"></param>
		<embed src="http://www.youtube.com/v/yGZdqLgKrLE&hl=en&fs=1&rel=0&color1=0x2b405b&color2=0x6b8ab6&border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="349"></embed>
	</object>
</div>
<p>In order to help you find what you need, you might consider using the search bar or browsing my categories in the sidebar. If you'd prefer to browse popular content or topics, they are listed below.</p>
<h3>Popular content:</h3>
<ul>
<?php akpc_most_popular(); ?>
</ul>
<h3>Popular topics:</h3>
<p><?php wp_tag_cloud(); ?></p>
<script type="text/javascript">
<!--//--><![CDATA[//><!--
	var GOOG_FIXURL_LANG = 'en';
	var GOOG_FIXURL_SITE = 'http://rickbeckman.com/';
//--><!]]>
</script>
<script type="text/javascript" src="http://linkhelp.clients.google.com/tbproxy/lh/wm/fixurl.js"></script>
<p>I hope you find what you are looking for!</p>
<p><?php if ($_SERVER['HTTP_REFERER']) { ?>I was automatically notified of the address of this page as well as the referrer address which contained the errant link, if there was one, so that I can see about fixing broken links. However, y<?php } else echo 'Y'; ?>ou are more than welcome to e-mail me regarding this error; I would love to hear from you, especially if you still need help finding something! My e-mail address is <a href="http://mailhide.recaptcha.net/d?k=01b-IllrVLyH5ns4ZAetQxxw==&amp;c=8-jNc3fhiknDjwtt2Xmz3g8AQdbKMET295VYd1WAoa0=" onclick="window.open('http://mailhide.recaptcha.net/d?k=01b-IllrVLyH5ns4ZAetQxxw==&amp;c=8-jNc3fhiknDjwtt2Xmz3g8AQdbKMET295VYd1WAoa0=', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300'); return false;" title="Reveal this e-mail address">rick...</a>@gmail.com.</p>

If your theme does not pro­vide you with a 404.php file, you could try cre­at­ing one within your theme folder and using the fol­low­ing block of code. It is based off of the error page for Kubrick, the default WordPress theme:

<?php get_header(); ?>

	<div id="content" class="narrowcolumn">

		<div class="post">

			<div class="entry">

				<h2 class="center">Content Not Found</h2>

<?php	if (!current_user_can('level_10')) {
	$referral		= $_SERVER['HTTP_REFERER'];
	$not_found		= $_SERVER['SCRIPT_URI'];
	$to				= get_option('admin_email');
	$subject		= 'Content Not Found @ ' . get_option('blogname');
	$content		= '';
	if ($_SERVER['HTTP_REFERER'])
		$content	= "Came from: $referral\n";
	$content		.= "Landed on: $not_found\n\nGet this fixed!";
	$headers		= 'From: ' . get_option('admin_email');

	mail($to, $subject, $content, $headers);
} ?>
				<p><span class="drop_cap">U</span>nfortunately, the content you are looking for could not be found. There are a variety of reasons why this could have occurred, but what is important is getting you back on track and hopefully to content that you will find to be useful.</p>
				<div class="video">
					<object width="425" height="349">
						<param name="movie" value="http://www.youtube.com/v/yGZdqLgKrLE&hl=en&fs=1&rel=0&color1=0x2b405b&color2=0x6b8ab6&border=1"></param>
						<param name="allowFullScreen" value="true"></param>
						<param name="allowscriptaccess" value="always"></param>
						<embed src="http://www.youtube.com/v/yGZdqLgKrLE&hl=en&fs=1&rel=0&color1=0x2b405b&color2=0x6b8ab6&border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="349"></embed>
					</object>
				</div>
				<p>In order to help you find what you need, you might consider using the search bar or browsing my categories in the sidebar. If you'd prefer to browse popular content or topics, they are listed below.</p>
				<h3>Popular content:</h3>
				<ul>
				<?php akpc_most_popular(); ?>
				</ul>
				<h3>Popular topics:</h3>
				<p><?php wp_tag_cloud(); ?></p>
				<script type="text/javascript">
				<!--//--><![CDATA[//><!--
					var GOOG_FIXURL_LANG = 'en';
					var GOOG_FIXURL_SITE = 'http://rickbeckman.com/';
				//--><!]]>
				</script>
				<script type="text/javascript" src="http://linkhelp.clients.google.com/tbproxy/lh/wm/fixurl.js"></script>
				<p>I hope you find what you are looking for!</p>
				<p><?php if ($_SERVER['HTTP_REFERER']) { ?>I was automatically notified of the address of this page as well as the referrer address which contained the errant link, if there was one, so that I can see about fixing broken links. However, y<?php } else echo 'Y'; ?>ou are more than welcome to e-mail me regarding this error; I would love to hear from you, especially if you still need help finding something! My e-mail address is <a href="http://mailhide.recaptcha.net/d?k=01b-IllrVLyH5ns4ZAetQxxw==&amp;c=8-jNc3fhiknDjwtt2Xmz3g8AQdbKMET295VYd1WAoa0=" onclick="window.open('http://mailhide.recaptcha.net/d?k=01b-IllrVLyH5ns4ZAetQxxw==&amp;c=8-jNc3fhiknDjwtt2Xmz3g8AQdbKMET295VYd1WAoa0=', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300'); return false;" title="Reveal this e-mail address">rick...</a>@gmail.com.</p>

			</div>

		</div>

	</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

Phew!

Thanks for read­ing, and I hope you’ll take the time to put together a truly use­ful con­tent not found error page for your users. It truly does help to think of such a page not as an error page but as a land­ing page which far too often isn’t taken advan­tage of. Your vis­i­tors are already on your site. They’re look­ing for some­thing; what are you doing to help them find it?

I would love to see what you come up with for your error pages, so you’re more than wel­come to post a link to your site (or directly to an error page) with some expla­na­tion of what you’ve done to pimp it out.

If you just post a link with­out any sort of expla­na­tion or con­text, it may not make it by my spam fil­ters (and if it does, I can’t guar­an­tee I won’t mark it as spam myself; a link isn’t conversation).

Now go out there and make the Web more usable! (And share this with at least one other web­mas­ter whose error pages could use a lit­tle TLC. ;-)

This entry was posted in Uncategorized. Bookmark the permalink.

48 Responses to 6 Steps to a Better 404 Page

  1. Donace says:

    Brilliant post man… I cur­rently use the Apache 404 plu­gin; this dis­play a nice Ajax based google SE and other goodies…though the amend­ments you have men­tioned will be going in! espe­cially the noti­fi­ca­tion code snippet.

  2. Donace says:

    I tried to add the snip­pet for the auto-​​email on a 404 and I get the email but not the url they land on…ie I get ‘Landed on’ and thats it

  3. Rick Beckman says:

    Donace: Well, that is strange; there may be vari­ances in server setups that use dif­fer­ent vari­ables for the page address. Any chance you could set up a file on your con­tain­ing only <?php phpinfo(); ?> (named some­thing like info.php or what­ever) and send me the address to the file? rick…@gmail.com

    I’ll take a look and see if there’s any way to get it work­ing for you.

  4. Donace says:

    cheers will do that…like the video on your 404 :p!

  5. Pingback: links for 2008-11-30 | girlie geek

  6. Pingback: Featured Post… This should be “Announcement”

  7. andi says:

    this is an EXCELLENT tuto­r­ial. Thank you for it Rick.

  8. andi says:

    Rick… how did you do the “sub­scribe to follow-​​up com­ments via email below? Is that a widget?

     — Andi

  9. Rick Beckman says:

    andi: It’s the awe­some Subscribe to Comments plu­gin, actually.

    Glad you enjoyed the tutorial. :)

  10. Pingback: Update - Links - Love - 2 | The Nexus

  11. Jeremy says:

    Great post. I found it from the handy link on Thesis Open Hook, and you make it easy to do the right thing. Had you con­sid­ered cre­at­ing a ver­sion that has the add and remove hook state­ments removed?

  12. joey says:

    Thanks, Rick. Can you please post one that has an Open Hook version?

    If not, can you please let me know how to do it? I’m really confused.

    Thanks so much,

    Joey

  13. Rick Beckman says:

    Use the code in the “For most other themes” sec­tion; the first code block can be used in OpenHook’s “404 Content” area.

  14. Ari Herzog says:

    OK, you rock, Rick, but you already knew that, right? :)

    I like your video touch but I’ve removed that for the time being. Not sure the best way to do that Google Search but­ton, but it’s bet­ter than the gnarly 404 stuff before.

  15. Ari Herzog says:

    OK, I’m hav­ing the same prob­lem as Doance in the above com­ments, Rick. This is to the T about the Twitter note I sent you.

    All I see in the email is:

    Landed on:

    Get this fixed!

  16. Mike says:

    Rick,

    I mod­i­fied the above and tried using it as a hook, but I think I am miss­ing some­thing sim­ple. The side­bar is now even with the bot­tom of the 404 page? I fig­ure I just have to close the hook some­how, but can’t quite get there.

  17. Nalco says:

    For peo­ple who are get­ting emails with a blank “Landed On:”, I fixed it by changing:


    $not_found = $_SERVER['SCRIPT_URI'];
    to
    $not_found = $_SERVER['REQUEST_URI'];

  18. Hi Rick,

    Do I need to do any­thing to cus­tomize this code to work on my site?

    Thanks!!

    if (!current_user_can(‘level_10’)) {
    $refer­ral = $_SERVER[’HTTP_REFERER’];
    $not_​found = $_SERVER[’SCRIPT_URI’];
    $to = get_option(‘admin_email’);
    $sub­ject = ‘Content Not Found @ ’ . get_option(‘blogname’);
    $con­tent = ”;
    if ($_SERVER[’HTTP_REFERER’])
    $con­tent = “Came from: $referral\n”;
    $con­tent .= “Landed on: $not_found\n\nGet this fixed!”;
    $head­ers = ‘From: ’ . get_option(‘admin_email’);

    mail($to, $sub­ject, $con­tent, $head­ers);
    }

  19. Rick Beckman says:

    You may need to make the change described in the com­ment directly above yours, but other than that, it should be fine, I’d think.

  20. alyssa says:

    Hi Rick,

    Everything is work­ing with the 404 code except the email noti­fi­ca­tion. Am I miss­ing something?

    Thanks!

  21. alyssa says:

    oh and I was won­der­ing if I can add this to OpenHook instead of the custom_​functions file.

    thanks!

  22. Rick Beckman says:

    Yeah, you can add it via OpenHook; just use the first block under “For Most Other WordPress Themes.”

    Not sure why the mail noti­fi­ca­tion isn’t work­ing, but note that if you’re test­ing it while logged in as admin­is­tra­tor, it isn’t going to trig­ger an e-​​mail. Only “not found” hits from non-​​admins will trig­ger the e-​​mail.

    Also, if your server/​host has an unusual setup that blocks, inter­cepts, or oth­er­wise ren­ders use­less the mail() func­tion­al­ity, then that would explain the lack of e-​​mails as well.

  23. alyssa says:

    Hi Rick,

    Thanks for get­ting back to me. I logged out as admin and that didn’t help. I spoke with Bluehost and the sup­port rep Tim said that he didn’t see a ref­er­ence to where the email should be sent. He thought that it was look­ing for the admin email, but that actual email isn’t present in the code.

    I’m still learn­ing about all this stuff, so I hope I’m say­ing all this correctly.

    Thanks!

  24. alyssa says:

    one more thing…
    what do I move to the 404 title sec­tion to get rid of the gnarly dude line?

  25. Rick Beckman says:

    Tell your sup­port rep to look again; the “To:” line should point to your WordPress admin address, as spec­i­fied by this line:

    $to				= get_option('admin_email');

    In the “404 Title” box you can put any­thing you want (no code, just text). “Page not found,” “You seem to be lost,” … what­ever you want! :D

  26. alyssa says:

    Hi Rick,

    Got the title working — thanks!

    I called sup­port again and they didn’t under­stand how the code is pulling the admin email so I’m still not receiv­ing the emails. I’ll keep work­ing on it. Not a crisis.

    Thanks for all your help :)

  27. Pingback: 6 Steps to a Better 404 Page — KingdomGeek — REIEntrepreneur.com

  28. My site en.petitbarzun.nl, is now replaced by petitbarzun.co.uk.

    So in an effort to inform users who land on the old page, I added a google script with

    var GOOG_FIXURL_SITE = 'http://www.petitbarzun.co.uk/';

    on the old site (en.petitbarzun.nl).

    Unfortunately, the Google script is not really using this vari­able, so it offers search­ing the old site rather than the new.

    Any sug­ges­tions?

    (KingdomGeek Edit: Markup fixed.)

  29. Rick Beckman says:

    I’ve never heard of that bit of script before; I’d rec­om­mend mov­ing all of your old con­tent to your new site and setup some .htaccess redi­rects to force users onto the new site seam­lessly. That’s what I do, anyway.

  30. Alyssa says:

    Hi Rick — me again.

    So I’m work­ing on another site and the email to admin with the 404 error is work­ing which is great. Problem is I’m get­ting all these error mes­sages when I view the site in IE 5.2 for mac. Here they are:

    Landed on: /wp-content/themes/thesis/lib/css/\‘images/dot-ddd.gif\‘
    Landed on: /wp-content/themes/thesis/custom/\‘images/hftpne_header.jpg\‘
    Landed on: /wp-content/themes/thesis/\‘images/icon-rss.gif\‘
    Landed on: /wp-content/themes/thesis/\‘images/submit-bg.gif\‘
    Landed on: /favicon.ico (there is no favicon.ico any­where in the site)

    and on and on.

    When I view the site in IE I can­not see my header and the nav­i­ga­tion bar is stacked rather than lay­ing horizontally.

    What do you think?
    Thanks!

  31. Rick Beckman says:

    The favicon.ico one is the result of the browser auto­mat­i­cally try­ing to grab a fav­i­con to dis­play. Only way to get around that from show­ing up in your logs is to cre­ate a fav­i­con for your site.

    For the oth­ers… It looks as though that ver­sion of Internet Explorer isn’t han­dling the apos­tro­phes sur­round­ing addresses in the style sheets. Not sure what to do about that one — using apos­tro­phes is the right thing to do.

    And for the nav menu, that’s likely because 5.2 for Mac is anti­quated and doesn’t sup­port a lot of what we take for granted nowadays.

    Less than two per­cent of Web users use IE 5.x, so it’s likely very much a non-​​issue, in my opinion.

  32. Hi,

    I have the code above in this web site and it’s work­ing great. I keep get­ting emails about an error from robots.txt. Do you know what that is and what I can do to fix it?

    Thanks!

  33. Rick Beckman says:

    That’s the result of robots attempt­ing to load your robots.txt file from the root of your site; to avoid the issue, just cre­ate a blank robots.txt file for them to load. The file itself can be a very handy tool in search engine opti­miza­tion, so check out a Google search for more infor­ma­tion if you want. :D

  34. ande says:

    Hi Rick
    You redi­rected me from Thesis Forums -
    As I said on there:
    I am get­ting about 20 – 30 emails say­ing Content not Found and then it gives a bogus address such as:

    - it adds the word ‘cat­e­gory’ in front of a post­ing when I never did this once:
    eg: Landed on: /​category/​2009/​02/​18/​nature-​​of-​​your-​​true-​​mind/​

    - adds an extended date that looks like its just dou­bled up:
    eg: Landed on: /​category/​2009/​02/​18/​2008/​07/​07/​how-​​you-​​can-​​feel-​​and-​​experience-​​mystical-​​love/​

    eg: — adds some sym­bol or non­sense word:
    Landed on: /​\” or
    Landed on: /​ftgtr

    I under­stand if I delete or move a post then I will get such mes­sages but these seem to be gen­er­ated with­out my input?

    Any help much appreciated

    Ande
    Web Support: Life Success and Spiritual Freedom | GuruZaz.Com

  35. Rick Beckman says:

    Welcome to the world of robots. Other than the search engines, there are hun­dreds if not thou­sands of types of bots out there that probe sites, look­ing for secu­rity weak­nesses, attempt­ing to iden­tify the soft­ware in use by test­ing out var­i­ous address forms, and all sorts of other things. These robots throw a 404 error which is then reported to you.

    You can mod­ify the code by wrap­ping the entire e-​​mail block like this: if ($_SERVER['HTTP_REFERER']) { e-mail-block-here } What that will do is send an e-​​mail only when a valid refer­rer is present, which rather than alert­ing you to every not-​​found request, alerts you when a link is fol­lowed from another page to a miss­ing page on yours.

  36. ande says:

    Thanks Rick — I do not know if I have done it cor­rectly but here is my code so you can see — notice I put the bracket you sug­gest before the $ and after the ] on the line ‘$refer­ral = ($_SERVER[’HTTP_REFERER’]);’

    func­tion custom_​404_​content() {
    if (!current_user_can(‘level_10’)) {
    $refer­ral = ($_SERVER[’HTTP_REFERER’]);
    $not_​found = $_SERVER[’SCRIPT_URI’];
    $to = get_option(‘admin_email’);
    $sub­ject = ‘Content Not Found @ ’ . get_option(‘blogname’);
    $con­tent = ”;
    if ($_SERVER[’HTTP_REFERER’])
    $con­tent = “Came from: $referral\n”;
    $con­tent .= “Landed on: $not_found\n\nGet this fixed!”;
    $head­ers = ‘From: ’ . get_option(‘admin_email’);

    mail($to, $sub­ject, $con­tent, $head­ers);
    } ?>

  37. Rick Beckman says:

    Try some­thing like this for that block:

    <?php	if (!current_user_can('level_10') && $_SERVER['HTTP_REFERER']) {
    		$referral		= $_SERVER['HTTP_REFERER'];
    		$not_found		= $_SERVER['SCRIPT_URI'];
    		$to				= get_option('admin_email');
    		$subject		= 'Content Not Found @ ' . get_option('blogname');
    		$content		= '';
    		$content	= "Came from: $referral\n";
    		$content		.= "Landed on: $not_found\n\nGet this fixed!";
    		$headers		= 'From: ' . get_option('admin_email');
    
    		mail($to, $subject, $content, $headers);
    	} ?>
  38. ande says:

    That is great Rick, it seems to have stopped the flow :-)

  39. Rick Beckman says:

    Yeah, it def­i­nitely will. You won’t get e-​​mails due to lost robots any­more. The mod­i­fi­ca­tion will ensure that you only get e-​​mails if some­one fol­lows a bro­ken link to your site, which will allow you to track down that link and try to get it fixed. :D

  40. Hi Rick,

    I added the code above to one of my sites and the emails have stopped. I am still get­ting a few from one of the oth­ers, but it’s a bit dif­fer­ent as there is a “came from” part now:

    Came from: http://hftp-né.org/meetings/Topics/HFTP%25252525252520HDTV%25252525252520discussion.ppt
    Landed on: /meetings/Topics/HFTP%25252525252520HDTV%25252525252520discussion.ppt

    Any sug­ges­tions?

    Thanks!

  41. Rick Beckman says:

    You added the code from one of my com­ments above? If so, that will pretty much stop 90% or more of the e-​​mails as it will only send out an e-​​mail if a refer­rer is present, which is the “came from” bit. These are the only use­ful e-​​mails that this was ever meant to send as it reveals where a bro­ken link to your page exists and should be fixed.

  42. Ok — I won­der where the refer­rer is com­ing from — I have never seen it before? Thanks!

  43. Rick Beckman says:

    The refer­rer is sim­ply the “came from” page. For instance, of you got to this page from a Google search, the search results page would be the refer­rer. Now, say you have a link to this page, but some­thing about the link is wrong (you had 405 instead of 404 in the address, for exam­ple). The link, when fol­lowed, would show a refer­ral from your site to the bro­ken address on my site. Knowing the refer­ring site would allow me to con­tact you to get the link fixed, which helps my site out.

  44. I under­stand what you’re say­ing — I was just won­der­ing what the refer­rer that is gen­er­at­ing that link was? According to the email, the refer­rer is the site itself, but even the old site did not have that address any­where. Strange right?

  45. Hi Rick,

    I have the pre­ceed­ing code and a robots.txt file, but I’ve been get­ting a ton of these lately:

    Came from: http://hftp-né.org/wp-admin/page.php?action=edit&post=9&message=1
    Landed on: /wp-content/plugins/events-manager/js/jquery-ui-datepicker/i18n/ui.datepicker-en.js

    Can you help me fig­ure out why? Is is related to the plugin?

    Thanks!

  46. Rick Beckman says:

    Looks like it. The plu­gin is ref­er­enc­ing a file which doesn’t exist. You can try re-​​uploading the entire plu­gin, which may fix the miss­ing the file, or you could con­tact the plu­gin ven­dor to see if the ref­er­ence is a bug that should be fixed or removed entirely.

  47. Ash Menon says:

    Hey Rick,

    Just wanted to thank you for the code. I tried it, and two things happened.

    1) I kept get­ting the email regard­less when I landed on ANY page in the server, and
    2) The “Landed on” value was empty.

    I’m not sure how I fixed the first one, but I removed some of the vari­ables and put them straight into the ‘mail’ script, to avoid extra bur­den on the server. The sec­ond one I fixed by chang­ing ‘SCRIPT_​URI’ to ‘REQUEST_​URI’. When I found that that would only out­put the page’s address rel­a­tive to my site, I added a text string with my site’s address before it, so my email noti­fi­ca­tion comes as the full link.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>