Welcome to CGI City - One of the biggest WWW Resources for CGI and Perl materials
Tips and Tutorials
Customizing your 404 and other error messages

Have you always wanted to create your own custom error messages to replace those boring black text on gray background generated by your web server? If this ever was one of the things listed in your "Things I want to do before I die", then you are in luck. This tutorial aims to teach you how to customize those error messages.

In the previous tutorials on .htaccess files, we discussed how it can be used to protect certain directories from unwanted visitors. We also discussed how it can restrict access only to selected visitors who know the secret password. It is already assumed that by this time, you already know how to create a text file on your server and name it .htaccess. We are also assuming at this point that you know enough about creating your own HTML documents as you will need this knowhow to customize your own error pages.

Step 1: Create your custom error page in HTML
Let's begin this tutorial by creating an HTML document containing a simple error message. Create an HTML file and name it notfound.html. In it, you may have something like this:

<HTML>
<HEAD>
<TITLE>Bob's Homepage: Error</TITLE>
</HEAD>
<BODY BGCOLOR=#FFFFFF TEXT=#000099>
<CENTER>
<H1>Sorry, but the page you are looking for <BR>
was not found in Bob's Homepage</H2>
<HR WIDTH=600>
</BODY>
</HTML>

You may add images if you wish but make sure you indicate a full URL to your image and not a relative URL. This ensures you don't get a broken image when the error page is invoked. Once you are though designing your custom error page, upload the finished HTML file to your web server on a directory of your choice.

Step 2: Modify your .htaccess file
What you'd like to happen is that whenever a visitor types in a wrong URL or hits a bad link on your site, that visitor would see your custom error message - notfound.html. This is accomplished with the ErrorDocument directive in your .htaccess file. Add this line into your .htacces file:

ErrorDocument 404 /notfound.html

In this example, the notfound.html file is placed in the web server's root directory such that if you wish to deliberately access it from the web browser, the URL is http://yourdomain.com/notfound.html. If you place it in another directory, make sure you specify the correct path to it.

Adding other error pages
You might want to customize an error page for every type of server error one might encounter. Here is a list of the common errors encountered:

Error Number Error
400 Bad Request
401 Authorization Required
403 Forbidden
404 File not Found
500 Internal Server Error

To customize your own error page to address one of these error types, simply follow Step 1 and 2 as you did with the 404 error. Then simply add a new line to your .htaccess file for a particular error type with the corresponding HTML file you created. Thus, your .htaccess file may contain something like this:

ErrorDocument 400 /badrequest.html
ErrorDocument 401 /authorization.html
ErrorDocument 403 /forbidden.html
ErrorDocument 404 /notfound.html
ErrorDocument 500 /internalserver.html

Note that where you place the .htaccess file plays an important role in the successful implementation of your error redirection. Remeber that it will only work for the directory where you placed the .htaccess file along with all the sub-directories beneath it.

Now, was that easy or what?

-oOo-

Back to Tips and Tutorials


Search For:
Quick jump to
Home | About Us | Contact Us | Link To Us | Search
Tips & Tutorials | References | Books | Add A Resource
Custom Programming Services | Advertise
Disclaimer | Privacy Policy

Copyright © 1998-2099    CGI City    All Rights Reserved
All original materials contained herein including text, images, and original scripts written by CGI City, are copyrighted by CGI City. All other scripts and materials are copyrighted by their respective authors/owners.