Skip to content

Tutorial: .htaccess

Custom Error Pages
One of the easiest things to do is to change the pages that are shown when an error is encountered. This also is a great way to enhance your site. A 404 (file not found) page with a site map showing your users where they can go is a good way to keep them even when they aren’t getting a page. How about a 403 error (forbidden) page that gives the user an option to register rather than just telling them they can’ t access the page? How about a 500 error (server error) that gives the user an opportunity to report the error to you?

First, you need to design a page that will replace the standards. I would highly suggest not using scripting languages, especially if you are using one for a server error. Straight HTML is best. Typically, I save all the error pages in the same place using the error number as a file name. For example, /errors/404.html would be my file not found page and /errors/500.html would be my server error file. Once these files are in place, we are ready to continue.

The directive we are going to be using is ErrorDocument. Simply add the line:

ErrorDocument ErrorNumber /file.html

to your page. So, for a file not found error you would use (replace the file with the path to your file):

ErrorDocument 404 /errors/404.html

This works for any error number. Some common errors are:

  • 400 – Bad Request
  • 401 – Unauthorized
  • 403 – Forbidden
  • 404 – File Not Found
  • 410 – Gone
  • 500 – Server Error

Pages: 1 2 3 4 5

Published inProgramming

Be First to Comment

Leave a Reply

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