APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Custom 404 Pages

© January 2000 Tony Lawrence
January 2000

Update: As I use different settings now, the suggested tests here WILL NOT WORK.

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:


There are a lot of things wrong with that link:

Now try this one:


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 https://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:

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

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

<h2>404- Page not Found</h2>
<!--#include virtual="/cgi-bin/rloge.pl" -->
<p><!--#include virtual="/cgi-bin/404.pl" -->
<a href="https://aplawrence.com/search.html">
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>


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:


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.

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

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Custom 404 Pages

Inexpensive and informative Apple related e-books:

Photos for Mac: A Take Control Crash Course

Take Control of Parallels Desktop 12

iOS 10: A Take Control Crash Course

Take Control of the Mac Command Line with Terminal, Second Edition

Take control of Apple TV, Second Edition

More Articles by © Tony Lawrence

Printer Friendly Version

Have you tried Searching this site?

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.

Contact us

Printer Friendly Version

Technology is a word that describes something that doesn’t work yet. (Douglas Adams)

Linux posts

Troubleshooting posts

This post tagged:





Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode