Apache .htaccess Guide, Tutorials and Articles

Apache .htaccess Guide & Tutorial >>

Error documents

Creating custom error pages is very useful, it allows you to show web site visitors a friendly error message, for instance if a URL on your web site does not work. This avoids the unfriendly '404 File Not Found' error and allows you to display a friendly error, explaining possible solutions and guiding the visitor back into your web site content, rather than leaving them frustrated and lost.

To set-up custom error documents, create a .htaccess file following the main instructions and guidance which includes the following text:

ErrorDocument 404 /error_pages/404.html

The above line tells the Apache Web Server to display the document located at /error_pages/404.html (under your domain name/web site address) whenever a 404 (file not found) error occurs.

In this example, we have assumed you have created the error document and called it '404.html' and that you have placed it in a directory called 'error_pages' under your domain name. For example, http://www.yourdomain.com/error_pages/404.html

The document 404.html is a normal HTML document like the others in your web site and can display whatever content you wish, however we recommend including a 'File Not Found' message.

To setup further error documents, for example for '401 Unauthorised', '403 Forbidden', and '500 Internal Server' error messages, create a .htaccess file following the main instructions and guidance which includes the following text:

ErrorDocument 401 /error_pages/401.html
ErrorDocument 404 /error_pages/404.html
ErrorDocument 500 /error_pages/500.html

It's all very well displaying a friendly error message, but more importantly you need to resolve the error. By using a CGI script instead of a static HTML document as the error document allows us to record errors in a database, and resolve them.

This can be achieved very easily thanks to a variety of pre-made solutions which can even show us which errors are received most frequently. Such products can be found on The CGI Resource Index and HotScripts.com.

Updated: 31st October 2007

Next Article: Redirects
Previous Article: How to use .htaccess

Comments

Jay
"Fantastic reading! Very simple, easy and effective. I was an utter noob before reading this and now I'm using .htaccess files on my site like a pro."
Moderator
"DC, yes of course, simply place another .htaccess file in the subdomain directory and this will cancel the previous rules. I believe you can simply use a blank .htaccess file, or alternatively setup a new set of error pages."
DC
"Within my domain, I have a folder for a subdomain. Is there a way to exclude this subdomain from using the main domain htaccess file? Basically, I want to use custom error pages for my main/top domain, but the subdomain includes an applet which will not work with custom error pages."
Moderator
"Yes, of course..."
Anonymous
"Is it possible to tell all ErrorDocuments to point to the same page?"
Moderator
"I don't think wildcards can be used, you will need to specify each error code individually. To auto-redirect after the user sees the error message, simply setup an error page with a meta refresh tag to redirect the user after X seconds. Error document files must be listed with a relative URL, you cannot use a full URL."
thanks
"are wildcards possible? to account for all 4xx or 5xx errors? auto redirect after a delay? ok to use a full url? (as i am making a .htaccess for a subfolder of a site user's site folder, so not sure about the proper relate path seen by apache) what font is your logo in i love it :)"

Post Comment

Your Name:
Your Comment: