Custom Error Page with .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

8 thoughts on “Custom Error Page with .htaccess

  1. Hi roshan,

    i have seen you site and found very helpful.

    Can you plz help me in .htacess setup.

    on my local machine i have setup one small news system and want to use user friendly urls like

    http://www.example.com/news-heading.htm

    remember here no id is shown here but when in coding i get this as “index.php?id=1″

    how is this possible?

    Secondly i have added the code for 404 error page and make 404.php file but not working?

    regards,
    Naseer

  2. I have used this code but doesn’t work for me….

    i have file 404.php on my root folder and use the following code in my .htaccess file

    **********************

    RewriteEngine on
    RewriteRule ^(.*)\.htm$ $1.php [NC]
    ErrorDocument 404 /404.php

    **********************

    can you tell me what the problem?

    regards,

  3. jayu

    Hello Roshan,
    I try your code 404 Error Redirection, But It doesn’t work for me. Use LInux server and mod_rewrite is on but when try to redirect its not working

    write my .htacess file

    Options +FollowSymlinks
    RewriteEngine on
    ErrorDocument 404 /404.html
    give me server error

    and if I write
    ErrorDocument 404 404.html
    not working just show me 404.html
    can y tell me where i am wrong?

  4. nice sharing I have used it onhttp://www.pnpzila.net and http://www.sbdpk.com
    thanks

  5. People above…
    Using examples of my file
    Change:
    ErrorDocument 404 /errors.php?code=404

    TO:
    ErrorDocument 404 http://www.mysite.com/errors.php?code=404

  6. Should clearify.
    Actually using your own pages, not errors.php?code=404

    if you have issues with it not working, put in your full URL.

  7. Neotropic, thanks for sharing….

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>