Web Server

Setup 404 Handler Using .htaccess

PHP have one great file called .htaccess file. With this file you can do lots of stuff which you might have not imagine.

In this post I will show you the trick to set up the 404 handler using .htaccess file.

What is 404 Handler?

404 handle is just like a fallback page, which should be display when user visited the page which is not exists.

How to set 404 handler?

For setting up the 404 Handler, you just need to perform two simple steps.

First is to create a file which handles the 404 error and place/upload this file to the root of your server.

Now place below code in your htaccess file.
[cc lang=”apache”]ErrorDocument 404 /404.php[/cc]
So from above code you can see that 404.php will be served when 404 Not Found occures.

So Easy :)

Shares:
  • Mehul
    July 18, 2011 at 8:43 am

    Really Nice post.

    Now my site has a very cool 404 page.

    Thanks for this trick. :)

    Reply
  • […] I have shown that how to set 404 handler using .htaccess. In this post I am going to cover other errors also with the same […]

    Reply
  • Yaeger Design
    December 22, 2011 at 8:03 pm

    What is the best way of returning a 404 HTTP status code. Can this be done in .htaccess, or must you set it in the HTML header?

    Reply
  • erle
    erle
    July 5, 2012 at 11:21 am

    Just an FYI. htaccess has nothing to do with PHP.

    This file is used as a directory level config file by many leading Webservers, Apache for example. PHP is a script language and has nothing whatsoever to do with this.

    In your config, you are actually setting the directive telling the webserver (most likely Apache) what to do in case of an error.
    http://httpd.apache.org/docs/2.2/mod/core.html#errordocument

    Reply

Leave a Reply

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