Error pages: love them or hate them, they’re a necessity; to warn our visitors when something has gone wrong and hopefully guide them in the right direction. Although web servers generally come with standard error pages, they tend to be nondescript, unhelpful, and to the non-tech user, often confusing!
Error pages are shown when the server responds with a specific HTTP status code. (Although not all status codes refer to errors!) There are lots of status codes, although the following errors are most common:
- 404 – occurs when a page can not be found
- 403 – occurs when an action is forbidden, typically seen when trying to view the contents of a directory when directory listings have been disabled
- 500 – occurs when the server can’t fulfil an otherwise valid request, e.g. when some silly billy messes up their .htaccess files
Error pages can be created like any normal HTML/PHP/etc web page — they can be customised with CSS and decorated (although probably unnecessarily) with fancy images. Ideally, each error page should feature the correct status code and a more specific statement explaining to the visitor what’s gone wrong. Need help writing useful error pages?
Once you’ve created your error pages, save them in the root directory of your web site (where index.html/php is stored) and give them sensible names like 404.html or 403.php — something sef-explanatory.
Once you’ve created the pages you need to create a .htaccess file; this is where you place the coding that tells the browser where to find your error pages. As most Windows Operating Systems will not allow you to create a file starting with a full stop (period), I find it easier to name the file htaccess.txt and then rename it after uploading it. So, create a file in Notepad called “htaccess.txt”.
In the htaccess.txt file, you need to type the following:
ErrorDocument 404 http://www.domain.com/404.html
Don’t forget to replace www.domain.com/404.html with the URL to the error file — wherever you’ve uploaded it — and 404 with the correct error number. If you’ve customised multiple error pages, place that same text onto a new line in the same document, replacing 404 with the number for each error page as well as the correct URL. For example, I have customised my 404 and 403 error pages, so my htaccess.txt file looks something like this:
ErrorDocument 403 http://www.jemjabella.co.uk/403.htm
ErrorDocument 404 http://www.jemjabella.co.uk/404.htm
Once you’ve added the lines which reference all of your new error pages, upload the htaccess.txt file to the root directory of your host and then you can rename it to .htaccess — including the “.” before the word htaccess. To test if a new 404 error page is working, simply type in your URL followed by /jklsdkls or some other ‘fake’ non-existent address and your 404 error should present itself. To test a 403 page, you will need to attempt to access a directory which has indexes disabled.