How to change the error message for the different error codes and redirect errors

When a client's request for a page cannot be fulfilled the client's browser displays an error message that corresponds to the particular error code. For example, it depends on whether the page cannot be found, whether access to it is forbidden, etc. You can find a description of some common error codes in the article on the most common HTTP error codes.

If you want to, you can change the default error message with any text of your choice. You can also redirect all errors from a particular error code to a page of your choice. You can do this by putting the ErrorDocument directive in an .htaccess file(s). If you want the directive to apply to the whole web-accessible content, put it in the .htaccess file that's in the public_html directory on your hosting account. If you don't know how to create or edit .htaccess files, check out the article on creating and editing .htaccess files.

To change the default message for an error code, put the directive followed by the code and the message in quotation marks. For example, let's say that you want to change the message for the 404 error code from Not Found to The page cannot be found. Then you need to put the following in the .htaccess file:

#Change the error message for 404 errors
ErrorDocument 404 "The page cannot be found"

You can also catch all errors from a particular error code and redirect them to a page. For example, you have a page called 404.html and you want to redirect all 404 errors to that page. Then assuming that the page 404.html is in the public_html directory on your hosting account, you need to put the following in the .htaccess file:

#Redirect 404 errors
ErrorDocument 404 /404.html

The path to the file to which you want to redirect the errors is relative to the root directory on your hosting account; the root web-accessible directory is public_html. So, for example, if the path to the page on your hosting account is public_html/new_folder/404.html, then the directive that you should put in the .htaccess file would be ErrorDocument 404 /new_folder/404.html.

You can also redirect errors to an external URL address. For example:

#Redirect 404 errors to HostKnox
ErrorDocument 404

However, you shouldn't redirect 401 errors to an external URL.

The above examples are for the 404 error code but you can use them with other error codes. The lines in the directives that begin with # are just comments that don't affect the directive itself. You can use such comments to remind you what the particular directive is for.

Was this answer helpful?

 Print this Article

Also Read