Web site error messages and creating custom error pages on Apache web server

Web site error messages and creating custom error pages on Apache web server cover image
  1. Home
  2. Apache
  3. Web site error messages and creating custom error pages on Apache web server

Before we look at custom error pages let me provide a background on errors that you can encounter while surfing the web. Error messages are generated by the web server software if something goes wrong. The server passes this information to a browser which then displays it to the surfer.

There are different kinds of web site errors. For example, a surfer might type a URL incorrectly, or a server-side script might misbehave, or visitors might try to access directories that they are authorized to do so etc.

Sponsored Links

The most common error message is the “Page Not Found” which is displayed when a surfer clicks on a malformed link or types in the URL incorrectly.

Any way, the error messages displayed by web servers are quite bland and offer no help to the visitor. In this article I shall guide you through the process of creating custom error pages for your web site – thus, beautifying the ugly pages displayed by the server. If you already know what custom error pages are, I suggest you read the next article which discusses what custom error pages should contain so that they are more meaningful to the visitor.

The different types of error messages

In addition to the 404 Page Not Found Error, other common error messages displayed by web servers are the 500 Internet Server Error, 400 Bad Request and 403 Forbidden message. You should, thus, create custom error pages for these 4 messages.

By the way, if you are wondering what the numbers (404, 500, 400 or 403) stand for – these are the HyperText Transfer Protocol status codes. You can find more information on them at HTTP codes list.

The image below is an error message for a 404 Page Not Found Error, displayed by a web server that hasn’t been configured for custom error pages.

Page with error message displayed by the Apache web server

Is the custom error pages feature already provided by your web hosting company?

Any web hosting company worth its salt will let you create custom error pages. In fact, most web hosting packages have a “custom error pages configuration” feature located under the control panel through which you can quickly create the pages. If you have it, you are in luck. Most of your work is already done. You just need to create custom error pages correctly (read the next article on proper way to creating custom error pages) and upload them to your web server.

However, if the quick custom error pages creation feature is not available on your web hosting package, you need to change the web server configuration. The following has been written for the Apache web server (the most popular server on the web).
Note: If you are not sure which server your web site runs on, you can always check this using Netcraft’s What’s that site running tool.

Configuring your server to display custom error pages

You can quickly have Apache display custom error pages by putting a .htaccess file in your root directory. The .htaccess (that’s htaccess with a dot prefix), is a configuration file used by Apache at the directory level. The directives (rules) in the .htaccess file customize those of the main configuration file. These rules work only for that directory and any subdirectories contained in it. That’s why if you place the .htaccess file in the document root, the rules will apply to all subdirectories on your web site. Finally, the directives placed in the .htaccess file need to be in a specific format and I shall be telling you about these shortly.

Creating and modifying a .htaccess file for Apache web server

First check if a .htaccess file already exists in web site root directory. You can confirm this by connecting to your web server using FTP. If you find a .htaccess file in the document root, download it and create a backup copy incase something goes wrong.

If there is no .htaccess file on your server, you can create one using any text editor. I suggest you use the simple Notepad text editor that comes with Windows. (On the Macintosh, you can use BBEdit and on Linux/Unix you can take the service of vi or pico)

In any case, open a new document in the text editor or open the .htaccess file (if there was one). Enter the following lines:

# Directives for custom error pages
ErrorDocument 400 400.html
ErrorDocument 403 403.html
ErrorDocument 404 404.html
ErrorDocument 500 500.html

The first line which starts with the hash sign (#) is a comments line. It can contain anything you want. The next four lines are the important ones. These are the directives for Apache to serve error documents in case anything goes wrong. Therefore, Apache should display 400.html, 403.html, 404.html or 500.html if it encounters the 400, 403, 404 or 500 error respectively.

The file names for your custom error pages are, thus, 400.html, 403.html, 404.html and 500.html.

You now need to save this file as .htaccess. If Notepad put the .txt after, do not worry, you can rename the file once you put in on the server using your FTP software. Remember the file name should be .htaccess – nothing before that and nothing after!

The final step is to upload the file onto the server. I suggest you use FTP. The .htaccess file needs to be put in the document root or server root directory – the directory that has the homepage of your web site.

We shall now look at how to properly create custom error pages for your web site.

Apache Hosting Web Design