Custom Error Page with .htaccess

July 27, 2009 | In: htaccess

In my last post of Hiding .php file extension with .htaccess Michael has mentioned that you can create your own custom page for the the 404(not found) error in server. And alex wanted to know that tip as well.Alex here is the post for you rather than replying your comment.

When you mistype the URL in a address bar for any website then you usually get the following error with the message.

404 Not Found

The requested URL /file_name.php does not exist.

Well 404 is error code in http which represents that “file is not found” in the server.In this article, You’ll learn the tips to show your own custom error page.

To display your own custom error page for 404 error code, create a .htaccess file in the root of the web server and place the following code in that file.

ErrorDocument 404 /errror404.htm

This would cause any error code resulting in 404 to be forwarded to yoursite.com/error404.html.

In the above line ErrorDocument is a server directive and 404 is the server status code and last part is the name of the error page created by yourself.

This is just a simple example of custom error document of the 404 error. If you want to create the custom error page for the codes 404 and 400 (bad request) then you can put the two following lines in .htaccess file.

ErrorDocument 404 /errror404.htm
ErrorDocument 400 /errror400.htm

Note : Never try to put a ErrorDocument for code 200 which mean that page is found and causes the infinite loop in the server.

You can see the various http status code in the http://en.wikipedia.org/wiki/List_of_HTTP_status_codes