Leon Anavi
IT Tips && Tricks

Administration

Created: 19.08.2009 16:27 Last Modified: 19.08.2009 16:39 Views: 7369
Keywords: .htacess, 404, custom, error, HTTP, server

Customize Web Server Error Pages at .htaccess File

HTTP Errors

Different types of errors may occur during browsing. The Hypertext Transfer Protocol (HTTP) has error codes for all of the problems that might occur. The most common HTTP status codes used for errors are:
  • 401 Authorization Required - Directory is protected by password
  • 403 Forbidden - The server has understood the request but rejects it.
  • 404 Not Found -The requested file is missing.
  • 500 Internal Server Error - CGI script error.
Refer to the HTTP standard to check the full list of status codes.

Customize Error Page

There is an option to customize all pages related to the HTTP error codes on your server in order to provide better user experience to the visitors. Locate the .htaccess file. By default for apache server with cPanel it is located at the www directory. Modify the file by adding instructions which files should be loaded in case of errors. Save the .htaccess file and uploaded it at the server (override the old file). Note you can change not only the file name but also the file type. For example to associate error 404 with a php script.

Example Configurations

Load a File on Error 404

Add the following content to the .htacess file:
#My custom 404 Error Page
ErrorDocument 404 /404.php
The first line is a comment. The second line defines that on error 404 the server must load file 404.php which is located at the same directory.

Load an URL on Error 404

The user can be redirected to another site.
#My custom 404 Error Page
ErrorDocument 404 http://example.com/

Display HTML

It is not mandatory to load a file or an URL. Another option is to display HTML content on error which is saved at the .htaccess file. Although this approach is working it is not a good practice.
#My custom 404 Error Page
ErrorDocument 404 <h1>error</h1>

Further Reading

HTTP 1.1 Status Code Definitions


  Home | About | Contact | Disclaimer | Sitemap © 2009-2022 Leon Anavi. All rights reserved.