(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



We no longer offer ftp downloads. If there is a file you need referenced here, please contact me by email and I will get it to you.

Custom 404 Pages

January 2000



Often people come to your web site with an incorrect URL, perhaps because they misremember something they saw before, or because someone else has accidentally misdirected them. If you have the ability to redirect 404 (page not found) errors to a custom page, you can help them find what they want.

To show that, let's try a deliberate error on my site: Click on this very incorrect link and see what you get:

HTTP://APLAWRENCE.COM//UNXIART/MODEMS.HTM.

There are a lot of things wrong with that link:

  • Everything is in upper case. That would work for the domain name, and it would work if my site were running on (shudder!) NT, but I run on Unix, so after the domain, the case has to be correct.
  • I don't have a directory "UNIXIART". I do have "Unixart".
  • Unix systems can use ".htm" extensions, but most of us don't.
  • I do have a "modems.html", but not MODEMS.HTM which is both case and the extension wrong.

Now try this one:

HTTP://APLAWRENCE.COM//UNXIART/DISKS.HTM.

That has the same problems as the first and more: I don't have an article called "disks.html" at all.

Yet in both cases, my custom 404 page gives enough information and suggestions that you could probably find what you wanted immediately.

Actually, in the first case "mod_speling" catches the mistype and fixes it.

Try http://aplawrence.com/unixaRT/MODemS.HTML. The only thing wrong with that is some incorrect case, which we can fix right up and actually put you on the right page (the Apache module "mod_speling" does the same thing ).

This happens first of all because the Apache server is configured to direct these types of errors to a particular place. This is the line in httpd.conf that does that:





ErrorDocument 404 /errordocs/filenotfound.html
 

See Apache Book Review for more information on configuring Apache servers.

That by itself can be a big improvement over the standard handling of errors. At least you can apologize for the error, and put in a link to to your search engine or a complete site map.

But I've taken it a bit farther. To do this, I used Server Side Includes (SSI). It's the "mime.types" configuration file that controls SSI parsing; here's the relevant line:

text/x-server-parsed-html       html shtml
 

Here's what the actual 404 error page looks like:

<html><head><title>
404-Page not found
</title>
<!--#include virtual="/cgi-bin/header.pl?/errordocs/filenotfound.html" -->
</head>

<body bgcolor="#ffffff">
<p><!--#include virtual="/cgi-bin/body.pl" -->

<h2>404- Page not Found</h2>
<p>
<!--#include virtual="/cgi-bin/rloge.pl" -->
<p><!--#include virtual="/cgi-bin/404.pl" -->
<a href="/search.html">
searching 
</a>
the site might help you find what you are looking for?  

<font face="verdana,arial" size=2>
<form action="/cgi-bin/search2.pl" method=POST>
Search for: <input text name=search size=20>
<input type=submit value="Perl Search"> </font>
 
<p>
 

</body>
</html>
</form>
 

It wouldn't look like that if you used "View Source" from your browser because the SSI's happen before your browser gets the page.

What happens is that the various SSI scripts ( the stuff that looks like <!--#include virtual="/cgi-bin/404.pl" --> ) get replaced with the text that the scripts output.

The important script for this page is the "404.pl" (the rest just control formatting and the site map at the top of the page). Here's what that looks like:

 

(you can download this Perl script)

The "trycase" subroutine corrects many incorrect case problems completely transparently.

Your imagination and, of course, your scripting ability are the only limits here. You could even automatically redirect the person if you are sure you know where they want to go, you could correct bad spelling, or you could do a complete site search and display the results. Whatever you do, it's better than just an error.

Also see the Search Engine Code

Publish your articles, comments, book reviews or opinions here!

© January 2000 A.P. Lawrence. All rights reserved





Click here to add your comments



Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



ad

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!



 I sell and support
 Kerio Mail server




pavatar.jpg
More:
       - Programming
       - Code
       - Web/HTML


Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress