Error 404 Pages

Today I got to thinking about those dreaded error 404 pages. They are really terrible for sites. I hate landing on a 404 page. Is that page gone? Moved? Is the entire site gone? Search engines hate them too. WordPress based sites like this one seem to handle missing pages fairly well but static web sites need lots of help. How do you handle them?

The first thing to do is fix any internal broken links you may have. I wrote a post about using a little program called Xenu to do just that here:

The next thing you need to do is create redirects for missing pages and directories. On a Apache web server you create a .htaccess file and include code like this for the missing pages and directories:

RewriteEngine On
RewriteRule ^missing_page.html$ /index.html [R=301,NC]
RewriteRule ^missing_directory/(.*)$ /index.html [R=301,NC,L]

In this case a specific removed file or directory are redirected to the sites main page.

Finally, once your site is cleaned up you need to do is create a custom 404 error page. This will catch any other missing files or miss-typed URLs. You want to make sure every one gets to your site instead of being left looking at a useless 404 error page. In the old days we created a simple HTML page with a “meta-refresh” tag on it that would just forward anyone hitting it where every we wanted them to end up. Because there has been so much Black Hat use of the meta-refresh tag search engines look down on sites with them. Another problem is duplicate content. If you have too many missing pages your 404 error page will get served up many times and be treated as duplicate content by Google which is very bad these days. Here is my solution.

Add this line to your .htaccess file:

ErrorDocument 404 /error404.php

This will cause the error404.php page to be shown anytime a missing page is asked for. You can change the file name to anything you like but it must remain a .PHP file to work properly.

Next create your error404.php file:

header(“HTTP/1.0 404 Not Found”);
<TITLE>Oops that’s a 404 good buddy!</TITLE>
<BODY TEXT=”#000000″ BGCOLOR=”#FFFFFF” LINK=”#0000FF” VLINK=”#0000FF”>
<script type=”text/javascript”>
var GOOG_FIXURL_LANG = ‘en’;
<script type=”text/javascript” src=””>
<H1>Sorry we lost that page :(<BR>
<FONT COLOR=”#FF0000″><a href=””>CLICK HERE to go to our main page.</FONT></h1>
<P>© Harris Enterprises 2011</P></CENTER>

The first bit of PHP code sends a result code: 404 (NotFound / Not Found) to the requester. This bit keeps your page from being counted as duplicate content. You can test your page at this site: to see what result code it is sending.

The rest of the page is just a standard HTML page. You can have fun with it any way you like. Blend it to your site, make it funny, the choice is yours. The important part is to have a link to your site. Usually to the main page but it can go wherever you want the traffic to go. I’ve included a little Google javascript widget that adds a search box that only searches your site. Not only is it a neat tool but doing Google searches on your site should attract Google’s spiders.

You can see what my sample error404 page looks like here. It is very plain but it works. I typically add a funny picture to my error pages.

This entry was posted in Websites and tagged , , , , , . Bookmark the permalink.

Leave a Reply

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