Skip Navigation
Site HomeSearch

On-Line Reference:
RedHat Linux Unleashed

Did this help?
... or
Donate at PayPal®...

 
Tux the Pengin

OK, here's a freebie: A simulation of the old text-based Star Trek game we used to play on HP 2100's using a Teletype ASR-33 as a console: StarTrek Game

Make a custom 404 page for your Apache server website

Prerequisites:

  • Your site must reside on an Apache-enabled webserver.
  • You must have FTP access to content on your site.
  • Your site must have the ability to use .htaccess overrides

Linking into a site and getting a "404 Not Found" page can be frustrating to your site visitors, creating a bad first impression. Much better is a custom 404 page that gives them some valid links on your site to prevent them from going away, showing you are web-savvy and have their interests in mind.

There are three steps to implementing your custom 404 page:

  1. Create the page html,
  2. Install an .htaccess file to redirect the 404 error to your custom page, and
  3. Test the installation and page links when you're done.

Step 1: Your custom 404 Not Found page

Your custom 404 page can be simple html or shtml markup, or a PHP or CGI script. I recommend starting with a simple html page such as this one:

<html>
<head>
  <title>Oops! | Saragossa.net</title>
<style type="text/css">
body {
  background: #f9fef9;
  padding-left: 25%;
  padding-right: 15%;
  padding-top: 5%;
  }
</style>
</head>

<body>
<h1>Oops! We couldn't find </h1>

<h2>
"<script type="text/javascript">
document.write(window.location.pathname);</script>
"</h2> <p>... perhaps you were looking for something under one of these topics?:</p> <ul> <li><a href="/">Some topic</a></li> <li><a href="/">Some topic</a></li> <li><a href="/">Some topic</a></li> <li><a href="/">Some topic</a></li> <li><a href="/">Some topic</a></li> <li><a href="/">Some topic</a></li> </ul> <p>If you really need help finding that URL, email <a href="mailto:you@your_domain">Your email address</a> </p> </body> </html>

The page above has the following recommended features:

  1. It is a simple html page leading off with a soft appology,
  2. A little standard ECMA script (JavaScript) shows the visitor what URL she was hunting,
  3. It provides a stack of links (sitemap) to topical locations on your site, and
  4. Your email address gives the visitor a last resort contact for help if she really, really needs to reach the URL she was originally attracted to.

In practice, you'd really want to obscure your email address to prevent site-bots from mining your address. Using a contact form would be event better -- link to that, if you have one. You can hide your address from simple, dumb 'bots by listing it as "you#AT#your_domain" or something such as that. A better way is to "encode" it using some JavaScript or PHP algorithm that breaks up the "mailto:" href, and further hides the actual address by substituting html entities for the characters in your address. There used to be a free tool at http://hivelogic.com/enkoder, but it looks like the author has obscured it, too.

Anyway, save that as "my404.html" or some name appropriate to your site.

Step 2: Tell Apache to redirect

Now you have to tell Apache to use your 404 page, not its default. You do that by creating (or having your site host create) a .htaccess file. This is a simple text file containing the following lines:

Options -Indexes
ErrorDocument 404 /my404.html

The first line isn't necessary, but it's a good idea; it prevents folks from seeing what's in your directory if something happens to your homepage. The second line tells Apache to use your 404 page instead of its default "Not Found" page.

Save that as a file called "dothtaccess", FTP it to your site's document-root (usually the directory called "www", "public_html" or "html" -- where your homepage is), then use FTP or SSH session to rename it to ".htaccess". If you aren't allowed to do that, have your site admin do it for you.

If you are one of several users on a local Linux LAN server in which you are allowed to have a site in your /home/*/public_html/ directory ("*" = your username), the Apache error document redirect must be a complete URL to work properly (Note, this is not the case for virtual-hosted sites; use the above method for those):

ErrorDocument 404 http://servername/~*/my404.html

Again, the "*" is your username on that server.

Step 3: Test the redirect

Bring up a web browser and type in a url you know doesn't exist in your domain, such as "http://your_domain/asdf.html". You should see your new "Oops!" page, not the Apache "404 Not Found". Typing "http://your_domain/" should give either your homepage, or an Apache 403 Access forbidden! error.

Hey! Why not use what you learned above to redirect to your own 403 error page!

Print plain