Apache .htaccess Guide, Tutorials and Articles
Username:
Password:
Register / My Account / Forgotten Password?

Error documents

(Edit this article)
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

<- How to use .htaccess Redirects ->

Visitor Comments and Responses

Moderator Article Rating: 10 out of 10
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.
Space Holder
DC Article Rating: 10 out of 10
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.
Space Holder
Moderator Article Rating: 10 out of 10
Yes, of course...
Space Holder
Anonymous Article Rating: 10 out of 10
Is it possible to tell all ErrorDocuments to point to the same page?
Space Holder
Moderator Article Rating: 10 out of 10
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.
Space Holder
thanks Article Rating: 10 out of 10
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 :)
Space Holder
Average Rating: 8.64 out of 10 (39 Votes)

Submit Comment/Rating

Article Rating:
Name: (optional)
Your Comment/Response: (optional)
Powered by OpenCrypt Article Manager
Table of Contents RSS Feed
1. What is .htaccess?
2. How to use .htaccess
3. Error documents
4. Redirects
5. Password protection
6. Deny visitors by IP address
7. Deny visitors by referrer
8. Hot link prevention techniques
9. Blocking offline browsers and 'bad bots'
10. DirectoryIndex uses
11. Adding MIME types
12. Enable SSI with .htaccess
13. Enable CGI outside of the cgi-bin
14. Disable directory listings
15. Setting server timezone
16. Changing server signature
17. Preventing access to your PHP includes files
18. Prevent access to php.ini
19. Forcing scripts to display as source code
20. Ensuring media files are downloaded instead of played
21. Setting up Associations for Encoded Files
22. Preventing requests with invalid characters
23. Useful Resources
 
Sponsors
Space Holder
Space Holder


Copyright © 1999 - 2008 ionix Limited. All Rights Reserved. Company Information, Affiliates

Powered by OpenCrypt Membership Software