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.

  • 2 utilizatori au considerat informaţia utilă
Răspunsul a fost util?

Articole similare

Modifying existing .htaccess files

To modify/add directives to the .htaccess file in your public_html directory using "File...

Introduction to .htaccess

What is .htaccess? The Apache Web server utilizes a per-directory access and configuration...

Enabling/disabling directory listings using .htaccess

By default when accessing any directory which does not contain an index file on a cPanel server...

How to redirect a page to another page or website using .htaccess?

If a page on your website no longer exists and you want to redirect it to your new page or...

Blocking users by IP address using .htaccess

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