Custom 404 Error Pages

A 404 error page occurs when a visitor requests a page that does not exist. The web server has an error code designated to every possible muck-up that could occur; 404 happens to be the one that means “file not found”.

The problem is, 404 error pages tend to look ugly. Often, your web browser will take over and give you its own rendition (which usually tells you even less than the web server’s version!)

The solution? Create your own. Any HTML document (or dynamic page, for that matter) can be specified as your 404 error page. You can usually do this with one of two ways:

  1. Through your web host’s control panel
  2. Through an htaccess file

You will need to check with your web hosting provider that you are able to do one of the above. Either way is fine, although you might like to learn how to htaccess files as a matter of course; they come in handy as you progress down the web development path.

When creating your 404 error page, make all file references absolute. That is, when you specify an image, use the full domain name (or path) in the SRC= attribute; when you link to another page, do the same. This is recommended because if a visitor requests a page that exists in a directory, the 404 error page will be loaded as if it resided in that directory, meaning images and links will be broken.

To specify the 404 error page using your web host’s control panel, consult their manual, or email tech support.

To specify the 404 error page using htaccess, do the following:

  1. Create an htaccess file if you do not already have one (consult the htaccess section for help with this).
  2. Add the following line:
    ErrorDocument 404 /404.html
  3. Upload the htaccess file to the directory on your web server that contains your HTML files. Ensure it is named “.htaccess” (no extension).

The ErrorDocument command takes two parameters; the error number (in this case, “404”) and the filename (in this case, “/404.html”). You can even specify a PHP script if you’d like to do advanced things with your 404 error page, such as show a site map, perform a search, try to guess the correct page, or email you an error report.

Custom 404 error pages are essential for a professional website, especially one whose pages often change. When selecting a web hosting company, be sure to check they support this feature.