Set Error Document with .htaccess

  • Avinash
  • 2
  • Sep 14, 2011
  • Web Server

.htaccess file has a great command over websites. You can perform lots of things without changing the web server’s configuration file.

Earlier I have shown that how to set 404 handler using .htaccess. In this post I am going to cover other errors also with the same trick.

General rule for setting the error handler.

ErrorDocument errorcode errorhandlerfile
 ErrorDocument errorcode errorhandlerfile

Now let’s see actual code to set the Error Handler file for each of the Server Errors:

#Bad Request ErrorDocument 400 /400.html #Authorization Required ErrorDocument 401 /401.html #Forbidden ErrorDocument 403 /403.html #Not Found ErrorDocument 404 /404.html #Internal server Error ErrorDocument 500 /500.html
 #Bad Request
 ErrorDocument 400 /400.html
   
 #Authorization Required
 ErrorDocument 401 /401.html
   
 #Forbidden
 ErrorDocument 403 /403.html
   
 #Not Found
 ErrorDocument 404 /404.html
   
 #Internal server Error
 ErrorDocument 500 /500.html

Above code will serve given file when the specific error occur. But you can set the HTML that needs to be server on specific error. Let’s see how to set HTML using .htaccess

You have to start html content with double quotation (“), but at the end closing quotation is not required.

#Forbidden ErrorDocument 403 "Forbidden, Back to home #Not Found ErrorDocument 404 "File not Found #Internal server Error ErrorDocument 500 "Something went wrong in server, Please correct
 #Forbidden
 ErrorDocument 403 "Forbidden, Back to home
   
 #Not Found
 ErrorDocument 404 "
File not Found
   
 #Internal server Error
 ErrorDocument 500 "Something went wrong in server, Please correct

Note: You can also set HTML for particular error from .htaccess file.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • orak

    When possible avoid having a .htaccess file and opt for configuration parameters to be in your web server configuration.
    .htaccess files are read for every requests, so there is a small overhead and disk usage, which might impact overall performances.

    • http://www.xpertdeveloper.com Avinash

      But you can have access to the server’s configuration file in case of dedicated or virtual dedicated servers only. So .htaccess will used in shared hosting environment.