What is a Custom 404 file and how do I make one?

by Ryan Smith October 20, 2009

Do you know what happens when you type a non-existent URL address?  You get an error message that probably looks like this:

Object not found!

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster.

Error 404

Not very pretty, right?  What is worse is that your readers might get confused and just go elsewhere instead of going back one page in your site.  You cannot prevent this type of error from happening.   Nobody can stop anyone from typing non-existent web pages in the address bar.  Well actually a 404 error can be created in three ways

  • A mis-typed URL (or a stale bookmark),
  • A search-engine link that is not there anymore, or
  • An internal broken link that escaped your notice

What we want to happen is to prevent the display of such types of error messages and replace it with something friendlier and easier to understand.  In other words, what we want to do is make our own HTML file that will become the generic page not found the handler that deals with this kind of error gracefully.  What are the bare essentials of a custom 404-page handler?

  • A link back to your home page
  • Most websites have search enabled, so if you have this feature, put this into your 404 handler
  • A link to your sitemap, if available

So how do we go about making our generic 404 error handler?  First, create an actual HTML file that contains all these elements.  Save it somewhere below your document root.  If you want to save it in a separate folder, then feel free to do so.

The next step is the tricky part and if done correctly should work in a Linux based server.  Try to look for the .htaccess file (please take note of the dot before htaccess).  If you have one, then we have to edit its contents, if not we have to create one. On a line of its own type something like this:

ErrorDocument 404 /404handler.html

This is all that is needed in order to create a custom 404 page.  The entry has 2 parts, first part tells us that if a 404 error happens, load the HTML file that is described in the second part of the line.  If a page not found error happens then load the file named 404handler.html. Simple eh?

Next, we will talk about a few tricks to make your 404 page really work.  If you type a link to your sitemap, make sure that you provide the absolute address.  While using HTML this link should look like this:

<a href=”http://www.yoursitename.com/sitemap.html”>Site Map</a>

When your visitors do something that leads them to a page not found error, they will see something they will understand, and that is one more visitor you keep in your site.

Ryan Smith
Ryan Smith was the former Director of Operations for McKremie.

Leave a Reply