Home Categories Design Tutorials Tutorial

Creating custom error pages using .htaccess

1.0/5.0 (1 votes total)

Michael Bloch

Michael Bloch
Michael Bloch
Team ThinkHost

ThinkHost is an international web hosting company offering reliable and well supported FrontPage, PHP and MySQL hosting services, plus a wide range of related resources for their clients.

Michael Bloch has written 2 articles for WebKnowHow.
View all articles by Michael Bloch...

o doubt you've been frustrated after visiting a web site, then clicking on a link only to be presented with the dreaded :

Error 404 - File not found

What's the first thing you do? In most instances I know I just leave the site altogether.

Lost visitor = lost $$

But in the instance that there is a properly structured custom error page, especially if it has a search box, I may hang around a while longer - or another offer may grab my attention.

If your web host supports .htaccess files, then with a few minutes work, you can have your own error pages up and running!

What is a .htaccess file?

It basically contains commands that instruct the server how to treat certain requests.

The .htaccess file contains a number of settings to control who can access the contents of a specific directory and how much access they have. It can also be used to create a "URL Redirect".

How do I implement custom error pages?

Create and publish what will be your custom error pages to your account as you would usually publish any page. You'll need to create two for the more common error codes, file not found (404) or unauthorized/forbidden (403, 401). Your custom error pages should have an apology, a brief blurb regarding what may have gone wrong (file renamed etc.). This explanation should be immediately followed by an invitation for the visitor to try reloading the page or to select a different section (provide suggestions). Ensure that these pages have your sites' look and feel.

After publishing the pages, you'll need to edit the .htaccess file in the root of your document directory of your site. Use the Edit utility (set to ASCII transfer mode) in your FTP software to view the file (it would be wise to also create a backup).

If you have a FrontPage web, be especially cautious, as the .htaccess file contains other important FrontPage configurations.

If you don't find a .htaccess file in the root of /docs, you can create your own with any text editor - ensure the file is called .htaccess (include the dot)

Add the following lines to the end of the file (change to suit)

ErrorDocument 404 http://blah/blah/404.htm
ErrorDocument 403 http://blah/blah/403.htm
ErrorDocument 401 http://blah/blah/401.htm

Save the file, test by trying to access a page that doesn't exist on your site - done!

Custom error pages are very easy to create and help you to retain wayward visitors - remember, that one visitor you may lose through not having custom error pages may be the one who was ready to buy your products.

What if I'm renaming or moving pages?

While you can use a custom error page in these situations, it's better to use a 301 redirect for a number of reasons. Learn why and how to implement a 301 redirect in our tutorial:


This article is copyright (C) 2003 and cannot be reproduced without express written permission from ThinkHost.

Add commentAdd comment (Comments: 0)  



Related Resources

Other Resources