6 Steps to a Better 404 Page

We’ve all experienced it. We’re reading a Web page only to come across a link which seems interesting enough to follow. 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 helpful error page, is it? What does “404” even mean to the average Web surfer? Sadly, the Web is littered with 404 error pages similar to what is quoted above. And what’s wrong with such error pages?

The Problem

  • They aren’t very friendly. Your visitors are human beings — talk to them like it. Treat them as your guests; they’ll be more likely to forgive the error and continue to browse your site!

  • They contain superfluous information. Seriously, why would Joe the Visitor care what server your site is hosted on unless he is a malicious user looking to exploit it? Why would Jane the Visitor care about “404” or other technojargon? I don’t even care about that sort of information, and I’m just geeky enough to know what it means!

  • They contain missing information. This may be a novel concept, but rather than simply stating that an error occurs, why not go out of your way to help your user get to something they’d enjoy viewing? No, I don’t mean automatically redirecting them to your current YouTube favorite. What I do mean is sharing with your users any of a number of possibly helpful items: a list of your popular content, a list of popular topics to browse, a freakin’ search box, or even a list of suggested content based off of the errant address which caused the 404.

The Solution

Your content not found page doesn’t have to be that way, though. It can be better — lots better! The following six steps are “best practices” in my opinion. I am using them here to create an error page which Chris Pearson says is “WIN for most helpful 404” (Twitter).

Your mileage may, of course, vary, and I encourage you to tailor 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 happened, why they are viewing an error page. Reassure your users that it is not their fault, and invite them to make use of the helpful resources you’re providing on the error page to get back on track. The faster they move from your error page to something more interesting, the less time the error page will have to affect the mentality of your visitor. “Oh yeah, I remember that site… 404s-ville.”

  2. Search: Always, always, always provide search functionality on your content not found errors. Let me repeat: Always provide search functionality on your content not found errors. You may have removed the article on “foo” that you users browsed to, but if they are able to search for “foo” on the error page, they may find a number of other helpful articles you’ve written on the subject. (This ties in to the thought that repetition is the best teacher and anything important on your blog probably bears blogging more than once.) It doesn’t matter if you already have a search bar in your sidebar; put one right in front of your user’s nose in the content of the error page itself. Don’t risk “sidebar blindness” rendering your error page less than useful.

  3. Popular content: Whether you’re tracking it or not, your blog has entries which is more popular than others. There is a reason for this: It it’s popular, your users like it! It only makes sense, then, that if your users get off the beaten path on your website, you would show them a list of popular content. If others have enjoyed it, why not them, right?

    In order to track popular content here, I make use of the Popularity Contest plugin for WordPress (Installing Popularity Contest on WordPress 2.5+). It allows me to use this block of code on my content not found page to output 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 popular posts (by visits, by comments, or by both via Popularity Contest), I highly recommend that you at least do something. Your users will appreciate it.

  4. Popular topics: There’s a chance that none of your most popular posts will tickle your users’ fancy, and that’s perfectly fine. Thankfully, for the past several versions, WordPress has allowed us to tag our content. You do tag, don’t you? Tagging our posts allows us to create a topical index of our content which can be far more useful than the basic categorical index. It also allows us to output a listing — in famous “tag cloud” form — of our most written about topics. The article on “foo” that your user was looking for may be missing, but they may find just what they need in your articles about “fubar.” Using this bit of code, you can add popular topics to your error page:

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

    You can further customize your tag cloud using the attributes listed in the WordPress codex!

  5. Google 404 widget: It’s hard to deny how useful Google’s wizardy is. From revolutionizing search to unleashing a legion of useful products such as Gmail, Reader, or Maps, Google is the one person in your life who you can exploit without fear of face-slaps!

    Recently, Google unveiled a new tool for webmasters to help increase Big G’s exposure webmasters make their sites more useful: the Enhance 404 widget.

    Basically, this new tool allows webmasters who have added their site to Google’s Webmaster Tools program — suave webmasters like you & I — to add a snippet of JavaScript to our content not found pages which allows Google to offer up some potentially useful tools. The tools which are output depends upon how much Google knows about your site, but the include things such as a Google search box, “closest match” suggestions, and a link to your site’s sitemap, if you have one.

    That’s pretty useful, in my opinion, 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 escaping is a method I learned from Jeff at Perishable Press.

    You may also notice that I omitted Google’s styling information. I moved it to my stylesheet and customized 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 mentioned earlier that the blame for a content not found error should not be placed on the user. A corollary to that would be knowing that the onus to fix the problem is upon you. As a webmaster, you should be willing to track down the reason content not found errors are happening, rectifying the problem(s) as soon as possible.

    If the problem is that the user has followed a bad link, then you’re going to want to get that link fixed. To facilitate this, I recommend adding a tiny bit of PHP code to automatically notify the admin e-mail address of your blog of the content not found error. This notification is completely anonymous for your users; only the broken address and the referral address (if there is one) are listed in the email.

    I also have the code set to ignore blog administrators so that you can test your error page without getting inundated with “Get this fixed!” e-mail messages.

    Here is the block of code to handle 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 encourage you to apply as much of this as possible to your error page, doing your part to make even the dark, erroneous underside of the Web that much more useful. If your users see that you took time to even make your error page useful, it will definitely set you apart from most other sites out there.

All Together Now

It may help to see the above used within the context of an actual error page, so I’m going to share the code I use to generate the content not found page in use here, video and all:

For the Thesis Theme

If you use the amazing Thesis theme, you’ll want to add the following block of code to your custom/custom_functions.php file. Don’t forget to customize 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 provide you with a 404.php file with which you can create your error page. If it does, open it and add the following block of code in the “content” section of the page; if you need help determining where that is, it may help getting in touch with the theme author or, if that isn’t practical, 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 provide you with a 404.php file, you could try creating one within your theme folder and using the following 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 reading, and I hope you’ll take the time to put together a truly useful content 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 landing page which far too often isn’t taken advantage of. Your visitors are already on your site. They’re looking for something; 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 welcome to post a link to your site (or directly to an error page) with some explanation of what you’ve done to pimp it out.

If you just post a link without any sort of explanation or context, it may not make it by my spam filters (and if it does, I can’t guarantee 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 webmaster whose error pages could use a little TLC. ;-)

45 thoughts on “6 Steps to a Better 404 Page”

  1. 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.

  2. 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.

  3. 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?

  4. 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

  5. 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.

  6. 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!

  7. 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.

  8. 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'];

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

  10. 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.

  11. 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!

  12. 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

  13. 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 :)

  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.)

    1. 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.

  15. 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!

    1. 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.

  16. 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!

    1. 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

  17. 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

    1. 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.

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

        1. 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: $referraln";
          		$content		.= "Landed on: $not_foundnnGet this fixed!";
          		$headers		= 'From: ' . get_option('admin_email');
          
          		mail($to, $subject, $content, $headers);
          	} ?>
    1. 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

    1. 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.

    1. 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.

  18. 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?

  19. 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.

  20. 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.

  21. 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

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

Use your Gravatar-enabled email address while commenting to automatically enhance your comment with some of Gravatar's open profile data.

Comments must be made in accordance with the comment policy. This site uses Akismet to reduce spam; learn how your comment data is processed.

You may use Markdown to format your comments; additionally, these HTML tags and attributes may be used: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.

the Rick Beckman archive
Scroll to Top