How to Respond to an HTTP Error Code Generated by your Server

http error codePicture this: A visitor to your web site is browsing through the site. He clicks on a link and suddenly comes face-to-face with the following error message:

404-error

The above is an example of a default or generic HTTP error page generated by most servers. It contains an HTTP error code (404 in the above example) and a brief description of the error (“Not Found” in our example). An error code, such as the one above, is generated by your web hosting server when it faces some problem or glitch with the transfer of web pages or other resources to your visitor’s web client (browser). There are different error codes for different problems. The 404 error code we see above is generated when the visitor clicks on a broken link (i.e., a link that points to a non-existing resource or page), or when the visitor types the address of a non-existing page while accessing your site. We’ll see some of these HTTP error codes in a later section of this article. You can learn more about HTTP and HTTP error codes in this tutorial on the Hyper Text Transfer Protocol.

The problem with the default HTTP error page generated by most servers is that other than the basic HTTP error code and brief description, there’s nothing else on the page. The rest of the page is totally blank. There is no link to take the user back to your home page. Nada. What do you think people who are not used to browsing the Web would do when faced with such a page? Most people will simply close the tab and move on to some other web site!

Another problem or issue with some default HTTP error pages is they expose the name of your hosting provider, which could be a security risk. A hacker, for example, could use this information to try and hack into your web site. (Though there are other ways a hacker can get information about your web site, why make it easy for him, eh?)

You can avoid all these issues and make your site more user friendly by displaying your own error page in place of the default error pages.

Displaying a User Friendly Error Message in Response to an HTTP Error Code

Most web hosts provide you with the facility to display your own error message in response to an HTTP error code, instead of their generic error pages.

Note: In the rest of this section, I will be describing how to set up the error pages for an Apache server. Though the steps may be slightly different, even non-Apache servers allow you to serve custom error pages. If your hosting account is a Windows Server based one, you can check out this excellent tutorial on Administering Windows Server 2012 for more details.

If you’ve got access to CPanel as part of your web hosting account, you can easily set up the pages using CPanel’s Error pages module (if your hosting provider has given you access to it). In that case, it’s just a matter of typing in the HTML or PHP code for the page—or copy/pasting it—in the provided editor. You can create different error pages in response to different HTTP error codes.

If, however, you don’t have access to the Error pages module, you will have to directly modify the .htaccess file on your server by using CPanel’s File Manager to browse to the root location of your web site (usually the www folder).

Use CPanel’s File Manager’s code editor to edit the .htaccess file, if it exists. If you cannot see an .htaccess file, create a file called .htaccess in the www folder.

Note: Though most commercial hosting providers will allow you to modify the .htaccess file, some of the free hosting providers may not allow you to do so. Contact customer support in case you face any difficulty in accessing or creating the .htaccess file.

As I’ve said before, there is a different HTTP error code for each problem the server faces. To create your own error pages, prepare a list of the error codes for which you want to serve custom pages, and add an ErrorDocument statement for each HTTP error code.

The syntax of the ErrorDocument statement is as follows:

ErrorDocument <HTTP Error Code> <name of error handling .html file>

For example, to create a custom response to HTTP Error Code 404, you need to add the following statement at the top of your .htaccess file:

ErrorDocument 404 filenotfound.html

You can then create the filenotfound.html file with a relevant (or even witty) message for your site visitor. Since this is a normal HTML page, you can include anything you want in this file.

Some common items added to these pages are:

  • Links to your home page or some other pages of your site.
  • A search box (so the user can search for the content he is interested in), if your site has a search engine.
  • A link to your sitemap so the user can find the correct page.
  • A “Report broken link” button.
  • Any other thing you want to keep the visitor on your site.

If nothing else, you should at least have a link to the home page so the user can continue browsing your site.

Common HTTP Error Codes

As I’ve said before, there are a myriad of HTTP Error Codes out there. The codes are grouped in different series, as listed in the following table:

 

The following table lists some commonly used error codes:

general-http-error-codes

To learn more about managing your Apache web server, check out this tutorial on Apache Web Server Administration.

You can prepare and serve a custom error page using the method outlined above for most of these errors. However, in some cases, you may need to follow a different procedure, but that is outside the scope of this article.