Thursday, May 2, 2013

How to create custom error pages with htaccess

Custom error pages enable you to customize the pages that are displayed when an error occurs. Not only will they make your website seem a lot more professional, but they can also save you some visitors. If a visitor sees a generic error page, they are likely to leave your site. However, if they see a helpful error page, they might just stay at your site because they can just click on a link to go to another page within your site. You can create error pages for all error codes, however many webmasters only make error pages for the 4 most common errors, which are:
Error 401 - Authorization Required
Error 403 - Forbidden
Error 404 - Not Found
Error 500 - Internal Server Error
To specify what the server should do when an error is found on your website, enter the following into an .htaccess file:
ErrorDocument  /home/LOGIN/DOMAIN/error-document.html
Change to the code of the error. Also, change the path to the error document. LOGIN represents your user name (the name you use to login through SSH or Telnet) and DOMAIN is the website domain in question. Simply repeat the above line of code for all other errors. Once the file is uploaded, your visitors will be directed to the page that you specified. Here's a sample .htaccess file with ErrorDocument enabled:
ErrorDocument 401 /401.html
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html
You can use full URL's for the path to your error documents on all error codes except 401, which must use a local path. Also, instead of specifying a URL for an error code, you can display a message too. Here's an example: ErrorDocument 404 "Sorry, the document you requested could not be found.
" This is quite useful if you only need to display a short message because it saves you having to create additional files. As you can see, you can use normal HTML code. Here's another .htaccess file with ErrorDocument enabled. This time, we are displaying messages instead of going to a different URL:
ErrorDocument 401 "Error 401

Authorization Required.

"
ErrorDocument 403 "Error 403

Forbidden.

"
ErrorDocument 404 "Error 404

Not Found.

"
ErrorDocument 500 "Error 500

Internal Server Error.

"

Disqus Comments