6 Steps to a Better 404 Page

by Rick Beckman on November 25, 02008

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 Prob­lem

  • 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. Seri­ously, why would Joe the Vis­i­tor care what server your site is hosted on unless he is a mali­cious user look­ing to exploit it? Why would Jane the Vis­i­tor 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 Solu­tion

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 Pear­son says is “WIN for most help­ful 404″ (Twit­ter).

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. Expla­na­tion: Explain to your users what has hap­pened, why they are view­ing an error page. Reas­sure 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. Pop­u­lar 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 Pop­u­lar­ity Con­test plu­gin for Word­Press (Installing Pop­u­lar­ity Con­test on Word­Press 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 King­dom Geek:

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

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

  4. Pop­u­lar 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. Thank­fully, for the past sev­eral ver­sions, Word­Press has allowed us to tag our con­tent. You do tag, don’t you? Tag­ging 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 Word­Press 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.

    Basi­cally, this new tool allows web­mas­ters who have added their site to Google’s Web­mas­ter 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 Per­ish­able 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. Noti­fi­ca­tion: 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 The­sis Theme

If you use the amaz­ing The­sis 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 Word­Press 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 Word­Press 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. ;-)

Publish Like a Pro with Thesis

What do you look for in a WordPress theme? Loads of options? Unsurpassed support? A thriving community?

Look no further than Thesis, the premier WordPress theme framework from DIYthemes.

Rock solid semantics. Bulletproof search engine optimization. Crystal clear typography. And more options than you can shake a stick at. What are you waiting for? Get Thesis today!

{ 45 comments… read them below or add one }

Donace November 25, 2008 at 18:38

Brilliant post man… I currently use the Apache 404 plugin; this display a nice Ajax based google SE and other goodies…though the amendments you have mentioned will be going in! especially the notification code snippet.

Donace November 28, 2008 at 13:09

I tried to add the snippet 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

Rick Beckman November 29, 2008 at 02:15

Donace: Well, that is strange; there may be variances in server setups that use different variables for the page address. Any chance you could set up a file on your containing only <?php phpinfo(); ?> (named something like info.php or whatever) 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 working for you.

Donace November 30, 2008 at 15:05

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

andi December 7, 2008 at 23:30

this is an EXCELLENT tutorial. Thank you for it Rick.

andi December 7, 2008 at 23:31

Rick… how did you do the “subscribe to follow-up comments via email below? Is that a widget?

– Andi

Rick Beckman December 8, 2008 at 00:01

andi: It’s the awesome Subscribe to Comments plugin, actually.

Glad you enjoyed the tutorial. :)

Jeremy December 15, 2008 at 13:31

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 considered creating a version that has the add and remove hook statements removed?

joey December 31, 2008 at 05:19

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

Rick Beckman December 31, 2008 at 15:13

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

Ari Herzog January 5, 2009 at 13:25

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 button, but it’s better than the gnarly 404 stuff before.

Ari Herzog January 5, 2009 at 14:28

OK, I’m having the same problem as Doance in the above comments, 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!

Mike January 25, 2009 at 00:09

Rick,

I modified the above and tried using it as a hook, but I think I am missing something simple. The sidebar is now even with the bottom of the 404 page? I figure I just have to close the hook somehow, but can’t quite get there.

Nalco January 28, 2009 at 23:29

For people who are getting emails with a blank “Landed On:”, I fixed it by changing:


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

Alyssa Suffredini February 9, 2009 at 19:15

Hi Rick,

Do I need to do anything to customize this code to work on my site?

Thanks!!

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);
}

Rick Beckman February 10, 2009 at 00:10

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

alyssa February 12, 2009 at 16:18

Hi Rick,

Everything is working with the 404 code except the email notification. Am I missing something?

Thanks!

alyssa February 12, 2009 at 16:19

oh and I was wondering if I can add this to OpenHook instead of the custom_functions file.

thanks!

Rick Beckman February 12, 2009 at 16:41

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

Not sure why the mail notification isn’t working, but note that if you’re testing it while logged in as administrator, it isn’t going to trigger an e-mail. Only “not found” hits from non-admins will trigger the e-mail.

Also, if your server/host has an unusual setup that blocks, intercepts, or otherwise renders useless the mail() functionality, then that would explain the lack of e-mails as well.

alyssa February 12, 2009 at 16:59

Hi Rick,

Thanks for getting back to me. I logged out as admin and that didn’t help. I spoke with Bluehost and the support rep Tim said that he didn’t see a reference to where the email should be sent. He thought that it was looking for the admin email, but that actual email isn’t present in the code.

I’m still learning about all this stuff, so I hope I’m saying all this correctly.

Thanks!

alyssa February 12, 2009 at 17:09

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

Rick Beckman February 12, 2009 at 20:00

Tell your support rep to look again; the “To:” line should point to your WordPress admin address, as specified by this line:

$to				= get_option('admin_email');

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

alyssa February 12, 2009 at 21:09

Hi Rick,

Got the title working – thanks!

I called support again and they didn’t understand how the code is pulling the admin email so I’m still not receiving the emails. I’ll keep working on it. Not a crisis.

Thanks for all your help :)

Pieter van Kampen February 22, 2009 at 05:14

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 variable, so it offers searching the old site rather than the new.

Any suggestions?

(KingdomGeek Edit: Markup fixed.)

Rick Beckman February 22, 2009 at 18:51

I’ve never heard of that bit of script before; I’d recommend moving all of your old content to your new site and setup some .htaccess redirects to force users onto the new site seamlessly. That’s what I do, anyway.

Alyssa February 23, 2009 at 19:24

Hi Rick – me again.

So I’m working on another site and the email to admin with the 404 error is working which is great. Problem is I’m getting all these error messages 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 anywhere in the site)

and on and on.

When I view the site in IE I cannot see my header and the navigation bar is stacked rather than laying horizontally.

What do you think?
Thanks!

Rick Beckman February 23, 2009 at 20:54

The favicon.ico one is the result of the browser automatically trying to grab a favicon to display. Only way to get around that from showing up in your logs is to create a favicon for your site.

For the others… It looks as though that version of Internet Explorer isn’t handling the apostrophes surrounding addresses in the style sheets. Not sure what to do about that one — using apostrophes is the right thing to do.

And for the nav menu, that’s likely because 5.2 for Mac is antiquated and doesn’t support a lot of what we take for granted nowadays.

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

Alyssa Suffredini March 23, 2009 at 15:02

Hi,

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

Thanks!

Rick Beckman March 23, 2009 at 17:48

That’s the result of robots attempting to load your robots.txt file from the root of your site; to avoid the issue, just create a blank robots.txt file for them to load. The file itself can be a very handy tool in search engine optimization, so check out a Google search for more information if you want. :D

Alyssa Suffredini March 23, 2009 at 18:19

Thanks very much!

ande March 27, 2009 at 23:28

Hi Rick
You redirected me from Thesis Forums –
As I said on there:
I am getting about 20-30 emails saying Content not Found and then it gives a bogus address such as:

- it adds the word ‘category’ in front of a posting 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 doubled up:
eg: Landed on: /category/2009/02/18/2008/07/07/how-you-can-feel-and-experience-mystical-love/

eg: – adds some symbol or nonsense word:
Landed on: /\” or
Landed on: /ftgtr

I understand if I delete or move a post then I will get such messages but these seem to be generated without my input?

Any help much appreciated

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

Rick Beckman March 28, 2009 at 00:52

Welcome to the world of robots. Other than the search engines, there are hundreds if not thousands of types of bots out there that probe sites, looking for security weaknesses, attempting to identify the software in use by testing out various address forms, and all sorts of other things. These robots throw a 404 error which is then reported to you.

You can modify the code by wrapping 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 referrer is present, which rather than alerting you to every not-found request, alerts you when a link is followed from another page to a missing page on yours.

ande March 28, 2009 at 01:09

Thanks Rick – I do not know if I have done it correctly but here is my code so you can see – notice I put the bracket you suggest before the $ and after the ] on the line ‘$referral = ($_SERVER['HTTP_REFERER']);’

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);
} ?>

Rick Beckman March 29, 2009 at 01:31

Try something 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);
	} ?>
ande March 29, 2009 at 04:52

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

Rick Beckman March 29, 2009 at 13:20

Yeah, it definitely will. You won’t get e-mails due to lost robots anymore. The modification will ensure that you only get e-mails if someone follows a broken link to your site, which will allow you to track down that link and try to get it fixed. :D

Alyssa Suffredini April 5, 2009 at 16:12

Hi Rick,

I added the code above to one of my sites and the emails have stopped. I am still getting a few from one of the others, but it’s a bit different as there is a “came from” part now:

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

Any suggestions?

Thanks!

Rick Beckman April 5, 2009 at 17:26

You added the code from one of my comments 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 referrer is present, which is the “came from” bit. These are the only useful e-mails that this was ever meant to send as it reveals where a broken link to your page exists and should be fixed.

Alyssa Suffredini April 6, 2009 at 09:20

Ok – I wonder where the referrer is coming from – I have never seen it before? Thanks!

Rick Beckman April 6, 2009 at 12:54

The referrer is simply the “came from” page. For instance, of you got to this page from a Google search, the search results page would be the referrer. Now, say you have a link to this page, but something about the link is wrong (you had 405 instead of 404 in the address, for example). The link, when followed, would show a referral from your site to the broken address on my site. Knowing the referring site would allow me to contact you to get the link fixed, which helps my site out.

Alyssa Suffredini April 6, 2009 at 14:16

I understand what you’re saying – I was just wondering what the referrer that is generating that link was? According to the email, the referrer is the site itself, but even the old site did not have that address anywhere. Strange right?

Alyssa Suffredini September 14, 2009 at 10:38

Hi Rick,

I have the preceeding code and a robots.txt file, but I’ve been getting a ton of these lately:

Came from: http://hftp-ne.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 figure out why? Is is related to the plugin?

Thanks!

Rick Beckman September 14, 2009 at 17:44

Looks like it. The plugin is referencing a file which doesn’t exist. You can try re-uploading the entire plugin, which may fix the missing the file, or you could contact the plugin vendor to see if the reference is a bug that should be fixed or removed entirely.

Ash Menon September 22, 2009 at 22:40

Hey Rick,

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

1) I kept getting the email regardless 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 variables and put them straight into the ‘mail’ script, to avoid extra burden on the server. The second one I fixed by changing ‘SCRIPT_URI’ to ‘REQUEST_URI’. When I found that that would only output the page’s address relative to my site, I added a text string with my site’s address before it, so my email notification comes as the full link.

Rob Mustard November 10, 2011 at 14:42

Hello, Rick! Thanks for the helpful info with setting up 404-trapping pages. I’ve been scratching my head over what I’m NOT seeing on my 404 error page: the Google search field (try http://www.daleyranch.info/asdf)! The fix url javascript is in the page, but it just isn’t showing the search box. Did I not do something to set up searching in Google’s Webmaster tools?

Thanks again (and in advance), Rick. I appreciate your help.

Leave a Comment

{ 4 trackbacks }

Previous post:

Next post: