Skip to Navigation

Home -> Articles & Tutorials -> .htaccess -> Custom Error Pages

Tutorial -:- Custom Error Pages -

Have you gone to a site clicked on a link and got a white page with '404 Error Page can not be found'

Boring aren't they. Well you can use .htaccess to enhance your site by providing custom error pages. The best use of this is to generate a page with links back to your home page.

If you want to create separate pages for all errors you need to know what the server is doing. When a web server can not deliver a page it generates an error code.

These are the standard codes

Successful Client Requests

200 OK
201 Created
202 Accepted
203 Non-Authorative Information
204 No Content
205 Reset Content
206 Partial Content

Client Request Redirected

300 Multiple Choices
301 Moved Permanently
302 Moved Temporarily
303 See Other
304 Not Modified
305 Use Proxy

Client Request Errors

400 Bad Request
401 Authorization Required
402 Payment Required (not used yet)
403 Forbidden
404 Not Found
405 Method Not Allowed
406 Not Acceptable (encoding)
407 Proxy Authentication Required
408 Request Timed Out
409 Conflicting Request
410 Gone
411 Content Length Required
412 Precondition Failed
413 Request Entity Too Long
414 Request URI Too Long
415 Unsupported Media Type

Server Errors

500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version Not Supported

You do not need to specify error pages for all of these, in fact you shouldn't. If you created an error page for 200 it would create an infinite loop.
In reality you would only create custom pages for 404 , 500, 401 and 403.

The 404 is what people see when they either click on a link to a page that no longer exists or is incorrect.

The 500 would help you out with internal server errors in any scripts you have running. You could put an email address on this page and ask users to report the error.

In my experience most users won't report an error but at least you won't loose them as you have put a link back to your home page on the custom error page. Haven't you. :)

401 - Authorization Required - As in when somebody tries to enter a protected area of your site without the proper credentials

403 - Forbidden - This is where a file requested has permissions set that forbid access or the user supply's the wrong username or password.

You might also consider 400 - Bad Request, which is one of those generic kind of errors that people get to by doing some weird stuff with your URL or scripts.

OK know you know a bit about what the server is going to do with each request lets start.

To define custom error page first you have to build them. You should always put a link back to your home page or at least to you sitemap on your error pages. You have got a site map haven't you?

This is the code that has to be inserted in to the .htaccess file.

ErrorDocument code /directory/filename.ext
or
ErrorDocument 404 /errors/404.htm

This would cause any error code resulting in 404 to be forwarded to
yoursite.com/errors/404.htm

Likewise with:
ErrorDocument 500 /errors/internalerror.htm

You can name the pages anything you want. I'd recommend something that would prevent you from forgetting what the page is being used for, and you can place the error pages anywhere you want within your site, so long as they are web-accessible (through a URL).

The initial slash in the directory location represents the root directory of your site, that being where your default page for your first-level domain is located. I typically prefer to keep them in a separate directory for maintenance purposes and in order to better control spiders indexing them through a ROBOTS.TXT file, but it is entirely up to you.

If you were to use an error document handler for each of the error codes I mentioned, the htaccess file would look like the following (note each command is on its own line):

ErrorDocument 400 /errors/400.htm
ErrorDocument 401 /errors/401.htm
ErrorDocument 403 /errors/403.htm
ErrorDocument 404 /errors/404.htm
ErrorDocument 500 /errors/500.htm


You can specify a full URL rather than a virtual URL in the ErrorDocument string (http://yoursite.com/errors/404.htm vs. /errors/404.htm). But this is not the preferred method by the server's happiness standards.

That's about it. Go to your site and type in a page that you know does not exist and you should get your new error page.

If you are feeling up to we will now move on.