Setup 404 Handler Using .htaccess

  • Avinash
  • 5
  • Jul 16, 2011
  • Web Server

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.

ErrorDocument 404 /404.php
ErrorDocument 404 /404.php

So from above code you can see that 404.php will be served when 404 Not Found occures.

So Easy :)

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • http://www.phpcodebase.com Mehul

    Really Nice post.

    Now my site has a very cool 404 page.

    Thanks for this trick. :)

    • http://www.xpertdeveloper.com Avinash

      @Mehul : Thanks

  • Pingback: Set Error Document with .htaccess | Expert PHP Developer

  • http://yaegerdesign.net/ Yaeger Design

    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?

  • erle

    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