A Nearly Perfect 404 Error Page in WordPress

2 min read · Posted on: Mar 1, 2006 · Print this page
In this seemingly infinite Web
Oh! young browser thou slips
In the middle of a thousand trips
Realising what was just
The apparent reality of pages
Is nothing but a veil of certainity
404 alone is the absolute reality
-Arun

So a visitor to your website mistypes a URL, what does he see? A nearly blank page with ‘404 error’ emblazoned over it? At least that was my case until I decided to do something about it.

So many great tutorials have been written on creating a 404 Error page. The best in my opinion is at SacramentoWeb. However, the solution the author has proposed leads the user to a search box already filled with a value by second guessing what the user tried to search for. I was looking for a 404 page that already has those results below the search box.

After some research, I finally hacked up some PHP code. The result can be already seen on my site and looks quite impressive. I mean I was quickly addicted to misusing the feature by performing random searches e.g. for hollywood stuff on my site I would try typing !!!

Here is a quick summary of what to do for any template to display these results:

  1. If you haven’t modified your .htaccess file, then open it and copy paste the following as your first line

ErrorDocument 404 /index.php?error=404

  1. Copy index.php from your template and rename it to 404.php in the same directory

  2. Remove all sections dealing with posts or comments. Roughly the lines between < ?php if ( have_posts() ) and ``

  3. Copy the following line to the first line of 404.php. This is to help search engine spiders indicate that this is an error page.

header(“HTTP/1.0 404 Not Found”);

  1. You can write pretty much anything in this page. Or you can download my 404 page from here

  2. Thats it!

Check out the results, I’m sure you (and your visitors) would be able appreciate the improvements in the browsing experience.


Arun Ravindran profile pic

Arun Ravindran

Arun is the author of "Django Design Patterns and Best Practices". Works as a Product Manager at Google. Avid open source enthusiast. Keen on Python. Loves to help people learn technology. Find out more about Arun on the about page.

Don't miss any future posts!

Comments →

Next: ▶   Decaffeination:: A Short Story

Prev: ◀   Forget Sleep: It's Unproductive

Up: ▲   Blog

Featured Posts

Frequent Tags

banner ad for Django book

Comments

powered by Disqus