Web Server

Set Error Document with .htaccess

.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.

[cc lang=”apache”]
ErrorDocument errorcode errorhandlerfile
[/cc]

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

[cc lang=”apache”]

#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

[/cc]

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.

[cc lang=”apache”]

#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

[/cc]

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

Shares:
  • orak
    orak
    September 15, 2011 at 6:15 pm

    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.

    Reply
    • Avinash
      September 15, 2011 at 10:32 pm

      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.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *