Custom Error Page

Mar 2, 2008 in Building Websites

What is .htaccess?

A .htaccess file is a ASCII file that you can create easily using a text editor like Notepad. .htaccess is the file extension of the file, it has no name or whatsoever, just ".htaccess".

To create the file, use a text editor and save the file as ".htaccess" without the quotes. Some text editors do not allow this as there is no file name. A simple way to bypass this is to save it as "file.htaccess" then use a free FTP software like SmartFTP to upload to your web server. From there, rename the file to ".htaccess" using the FTP software.

This is usually not an issue, but check the CHMOD of .htaccess you uploaded. It needs to be set to 644 (RW-R-R). Most FTP softwares allow you to CHMOD. For SmartFTP, just right-click the file and select "Properties / CHMOD". This allows the server to access the file and prevents any other external access.


Using .htaccess

.htaccess is one of the most important files in Apache. There are so many things that can be done via .htaccess. This file affects the entire directory it is place in. This includes the sub-directories of the directory. You can have a .htaccess file in each directory and the most immediate one will override the commands of the previous one.

So if you have a .htaccess in the root directory (public_html), it will affect the whole website. Place it in a sub-directory (/example) and it will only affect the files in that directory.

It is important that you use .htaccess only when needed. Uploading them in every directory you have is not a good idea as you might set off a endless redirect, causing the browser to stop loading your website.

Before using .htaccess, you must ensure that your web host allows you to use one. Also, some functions might be disabled by the web host for security and server stability purposes. So if any commands are not working, you might want to contact your web host. There are many functions you can use with .htaccess. Read Mod_rewrite with .htaccess for more advanced functions.


Error Documents

When your server returns an error, you will see one of those ugly looking default webpages. These webpages are called error documents. One of the most popular command used for .htaccess is to have your own custom error documents. You can specify which webpage to throw up when the server returns an error.

There are many types of errors that can be returned. See below for the full list of error codes. The most common ones are the 404 and 500. You will want to make a custom error page for these two, or at the very least, for 404. 404 occurs when the user cannot find a particular webpage. This is usually due to a broken link on your website.

Use the following command for .htaccess

ErrorDocument 403 /errors/forbidden.html ErrorDocument 404 /errors/page-not-found.html ErrorDocument 500 /errors/server-error.html

You can replace the numbers (403, 404, 500) to any of the error codes to create an error document for it. The webpage after the error code (/errors/page-not-found.html) points to your custom webpage that you have to create and upload. When the error is encountered, these custom webpages you specify will show up instead.

Do not make an ErrorDocument for all of the error codes. It is not neccessary and in fact, doing so for code 200 will trip off an infinite loop.


Error Codes

Successful Client Requests
200 	OK
201 	Created
202 	Accepted
203 	Non-Authorative Information
204 	No Content
205 	Reset Content
206 	Partial Content

Client Request Redirected
300 	Multiple Choices
301 	Moved Permanently
302 	Moved Temporarily
303 	See Other
304 	Not Modified
305 	Use Proxy

Client Request Errors
400 	Bad Request
401 	Authorization Required
402 	Payment Required (not used yet)
403 	Forbidden
404 	Not Found
405 	Method Not Allowed
406 	Not Acceptable (encoding)
407 	Proxy Authentication Required 	 
408 	Request Timed Out
409 	Conflicting Request
410 	Gone
411 	Content Length Required
412 	Precondition Failed
413 	Request Entity Too Long
414 	Request URI Too Long
415 	Unsupported Media Type

Server Errors
500 	Internal Server Error
501 	Not Implemented
502 	Bad Gateway 	 
503 	Service Unavailable 	 
504 	Gateway Timeout 	 
505 	HTTP Version Not Supported

If you like this article, Share It!

MetaCafe is paying video contributors $5 for every 1000 video views. You must own the rights to the video to earn from their Producer Reward Program.