Redirect your Traffic for Error handeling

In this tutorial we will make a .htaccess file that helps to redirect traffic in case of error.
redirect

In last tutorial we discussed planed redirects as for maintenance etc. Here we will do the redirect for error handling.
Errors are common things in all kinds of computer programs. The best way to eliminate them is to be to clean coding and other ways, but ´ a good practice to add error handling capabilities to all programs to inform the user about the error and the possible solution to solve the error.

In web world the common errors are 404 not found, when a file is missing, usually caused by broken links or user types some mistake in URL.
The second one is 500 services unavailable, when your server machine has some problem like connecting to MySQL server or with PHP or ASP or JSP configuration, even error in .htaccess can cause this.We will do the codes for these and a few other errors too

These are usual in real world, the key step to overcome this is to ask the user to inform you, but not everyone will do that unless you provide him with a contact form when the error occurs, though a few skip that but still the user gets that ´ an error. Here is the code description for common error that will redirect the user to another page.

THE CODE

The common code for all error document is as follows, all you need to do is to put this code in .htaccess and changes to be made is to code and to the file name that you want the user to be redirected.


ErrorDocument code /directory/filename.ext                
                

For detailed description on Status codes see below

In order to specify your own ErrorDocuments, you need to be slightly familiar with the server returned error codes. (List to the right). You do not need to specify error pages for all of these, in fact you shouldn’t. An ErrorDocument for code 200 would cause an infinite loop, whenever a page was found…this would not be good.

You will probably want to create an error document for codes 404 and 500, at the least 404 since this would give you a chance to handle requests for pages not found. 500 would help you out with internal server errors in any scripts you have running. You may also want to consider ErrorDocuments for 401 – Authorization Required (as in when somebody tries to enter a protected area of your site without the proper credentials), 403 – Forbidden (as in when a file with permissions not allowing it to be accessed by the user is requested) and 400 – Bad Request, which is one of those generic kind of errors that people get to by doing some weird stuff with your URL or scripts.

Try to make a creative 404 page so that user does not gets irritated , You can even use a PHP file , use flash or css for making the page more attractive at the same time convey the message of the error and don?t for get to keep a link to inform you about it. This will make them happy.

Code

The codes to be added to .htaccess for common error like , 400,401,403,404 and 500 are as follows


ErrorDocument 400 /badrequest.html
ErrorDocument 401 /authreqd.html
ErrorDocument 403 /forbid.html
ErrorDocument 404 /notfound.html
ErrorDocument 500 /serverr.html                
                

Here we have used .html exrension but any extension is possible.

Do not use a page with total size greater that 200Bites -300 Bites for 500 error Document, as the error is cased due to server problem , making the page heavy can make it more Worse. So .html is recommended for 500 error Document.

Use HTML TO MAKE IT EASY


ErrorDocument 500 "

You have managed to crash our systems.


We were joking ´ a 500 error "

Server Status 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