Wednesday, September 5, 2007

Creating custom error pages using .htaccess

Creating custom error pages using .htaccess

Most of the times while visitng a non-existing url on the webpage, you can see an error like

Error 404 - File not found

But in the instance that there is a properly structured custom error page, especially if it has a search box. If your server supports .htaccess files, then with a few minutes work, you can have your own error pages up and running!

Definition of 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 to 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.