How do I setup custom error pages using .htaccess?

This article covers the use of a .htaccess file to change Apache's settings directly to display a custom error page.

One big advantage of using .htaccess files to manage errors is that you can redirect them to a script, instead of just providing static pages. For example, our .htaccess file redirects errors to a PHP script which then sends our Support Team an e-mail, informing them of the problem. Using this we can find missing pages, bad links and general problems with our web site very quickly.

How to Setup

Setting this up is quite easy. However, you must first have the page(s) or script available otherwise you will get the error (e.g. 403 error) and a 404 error as well, as it couldn't find the page you told it to display.

For each error you want to redirect, add the following directive to a .htaccess file:

ErrorDocument 000 /path/to/filewhere 000 is the error number (e.g. 400, 404 or 500) and /path/to/file is the location of the error document on the web site (not on the hard-drive). For example, if you have created an error page at http://example.com/error/404.html, then just use the path after the domain (example.com): /error/404.html, i.e

ErrorDocument 404 /error/404.html


You can also point it to a script with options, e.g.

ErrorDocument 404 /error.php?err=404

 

Finally, you will need to do this for each error you want to report. Any errors not covered by this method will fall back either to the Custom Error Documents options (if set, and option avilable), or to the default reporting method in Apache itself. For example, the following file will cover most errors:

ErrorDocument 400 /error/bad-request.html
ErrorDocument 401 /error/unauthorised.html
ErrorDocument 403 /error/forbidden.html
ErrorDocument 404 /error/file-not-found.html
ErrorDocument 500 /error/internal-server-error.html



  • 3 Users Found This Useful
Was this answer helpful?

Related Articles

What is Softaculous?

Softaculous is a commercial script library that automates the installation of commercial and open...

How to upload (Linux Hosting) ?

In this tutor you're about to learn how to upload your website content via FTP client software...

What is www or public_html folder?

Please note that " www"  is just the symbolic link or just a mirror to the public_html directory....

What is .htaccess?

The .htaccess file can be placed in one or more of your /home/user/public_html subdirectories....

How to enable 'register_globals' ?

Since we upgrade all our Linux server to suphp enable, The php_value or php_flag configured will...