Implementing Sensible 404 Pages With Apache - Custom error documents
(Page 2 of 4 )
If your hosting provider runs off of Apache web servers, then all you need is an .htaccess file within the root directory of your web site. Windows based web-hosting companies, however, will often use IIS, or Internet Information Server, where .htaccess files are not supported. Many hosts will offer a control panel, allowing the Webmaster to easily control their web site, modify settings, implement features any many other tasks. If you are unsure about whether or not your host runs off of Apache or IIS, check your control panel, if you have one, for a custom error document option. If your control panel does not contain the ability to setup error pages, or your host does not support control panels, consult with your host's technical support department and inquire about how to set one up. Since custom error document support through Apache is universal, the following will detail how to perform this action.
Setting up error documents with Apache We will be working with a .htaccess file, which is a file that runs in the background and contains particular options and settings for your web site, one being custom error document codes and locations.
[Note] Some web servers will not display files beginning with a period (.), or dot. Because of this, you will have to search for the document or manually type the document name, in this case .htaccess, into the location bar within your FTP program.
[End Note] Firstly, fire up your FTP program and login to your account through your host. Next, search for a .htaccess file. If you cannot find a .htaccess file on your web site, do not fret. .htaccess files are a snap to create, and we will do just that below.
Creation of .htaccess files can be done with a normal text editor, such as Notepad. You may also use other editors like Editplus, Homesite, etc. To create a .htaccess file with the 404 error document supported, all we need to do is type one simple line of code at the top of the page:
ErrorDocument 404 http://www.yourdomainname.com/custom404page.html
The above line of code contains three different parts. The first part, ErrorDocument, states that you are defining a custom page for one particular error code. The second part, 404, is the actual error code. The third is the location of the custom 404 page. All you need to do is change the location of your error document to your own file on your web server, and you are set. Be sure that the error document (i.e.: custom404page.html) is available on your web server. Save the file as '.htaccess', without the single quotes, of course, and transfer the file over to your web server, usually in your root directory.
Pop quiz. What do you suppose the line would read if you'd like to specify a 500 Internal Server Error page? Yes, that's right:
ErrorDocument 500 http://www.yourdomainname.com/custom500page.html
Next: Designing the error document >>
More Apache Articles
More By Steve Adcock