Custom Error Pages


Custom Error Pages A number of errors can happen when you try to access web pages on a website. One example is trying to access a web page that doesn’t exist on a website. In this case, the web server program that returns the web page you requested will send you an error message instead.

When you try to access a web page that doesn't exist, the web server could send you a default error message like: '404 not found', 'HTTP 404 not found', 'The requested URL was not found on this server', and other similar messages.

The default messages are not always user friendly, and generally won’t follow the look of your website. That is where 'Custom Error Pages' come in - you define the error pages the user sees.

.htaccess
The information about the each of the custom error pages you create is included in the .htaccess file. The .htaccess file contains a series of commands and is used by the web server to control operations on the website. Another area that the .htaccess file is used for is password protecting directories.

The .htaccess file is a plain text file, and is named exactly .htaccess (just the extension with no name in front, and all lowercase). Each line in the file usually has one command, and the file needs to be uploaded to the website as a TEXT file and not BINARY.

.htaccess files affect the directory (and all subdirectories) of the directory they are placed in. If you place the file in your root directory – (example: www.thewebsite.com/.htaccess), it would affect the whole website.

If a separate .htaccess file is placed in a subdirectory of a directory that already has an .htaccess file, it will be used for that subdirectory (and it’s subdirectories) instead.

You can create a web page for each error you would like to handle. They can be named anything you want, and it is convenient to put them in a subdirectory of their own (although you don’t need to). An example of a subdirectory could be: www.thewebsite.com/errors


Some of the Errors you could create pages for:

400 Bad Request
The request cannot be fulfilled due to bad syntax.

401 Unauthorized
Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided.

403 Forbidden
The request was a valid request, but the server is refusing to respond to it. Unlike a 401 Unauthorized response, authenticating will make no difference.

404 Not Found
The requested resource could not be found.

500 Internal Server Error
A generic error message, given when an unexpected condition was encountered and no more specific message is suitable.

The command that is used in .htaccess for an error is named ErrorDocument.

The following commands are examples that could be used for the previous errors mentioned.

ErrorDocument 400 /errors/bad-request.html
ErrorDocument 401 /errors/authorization-required.html
ErrorDocument 403 /errors/forbidden.html
ErrorDocument 404 /errors/notfound.html
ErrorDocument 500 /errors/internal-server-error.html


The ErrorDocument line has a number that is related to the error, and the file to display is listed next to the error number.

In this case the files that are created are: bad-request.html, authorization-required.html, forbidden.html, notfound.html, and internal-server-error.html.

The files are then placed in the /errors directory.

Example: www.thewebsite.com/errors/notfound.html



pdfPDF Version