Creating custom error pages using .htaccess

One of the many things one can do to make their website a little more professional-looking is to create custom error pages which will be displayed instead of the default generic error pages such as "404 - File Not Found". In addition to there being practical uses to this (example: one can include an option to allow users to notify you of dead links or even setup a script to notify you automatically) an attractive looking custom error page can make even your mistakes look good.

While the HostRocket Control Panel includes a "Custom Error Pages" tool which can be used to easily setup custom error documents, if preferred, one can also accomplish this manually by modifying the .htaccess file in their public_html directory.

The most commonly encountered error messages are:

400 - Bad Request
401 - Authorization Required
403 - Forbidden
404 - Not Found
500 - Internal Server Error

In order to have custom error pages that you create yourself be displayed instead of the default server error, one would add directives similar to the following to their /public_html/.htaccess file:

ErrorDocument 400 /400.html
ErrorDocument 401 /401.html
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html

The above assumes that one names the files ###.html where ### represents the number of the error (one can name the files anything they wish and modify the directives accordingly) and places the files within their public_html folder. If storing the files within a subdirectory is preferred this can also be done. The following example shows the appropriate directives to use if one were to upload their custom error pages to a subdirectory named "err":

ErrorDocument 400 /err/400.html
ErrorDocument 401 /err/401.html
ErrorDocument 403 /err/403.html
ErrorDocument 404 /err/404.html
ErrorDocument 500 /err/500.html

Things To Note

In Microsoft Internet Explorer version 5.x and later, by default Microsoft's own "friendly" error pages will be displayed unless your custom error document is 512 bytes or larger. So in order to make sure that your error pages are displayed make sure that the size of all custom error pages exceed 512 bytes.

  • .htaccess, web development, error page
  • 2 Uživatelům pomohlo
Byla tato odpověď nápomocná?

Související články

Preventing bandwidth theft using .htaccess

While all HostRocket plans include plenty of bandwidth (transfer) nobody ever has enough...

Blocking users by IP address using .htaccess

Should you find that a few unruly visitors are causing you problems by posting unwanted messages...

Directory protection using .htaccess

One of the more common/popular uses of .htaccess is to password protect specific directories...

Redirection using .htaccess

Most websites are constantly changing "works in progress" and as one updates, rearranges and...

Setting a default index page using .htaccess

By default our servers will look for (in order) a file by the name of index.html, index.cgi,...