CodeIgniter Custom 404 Page

There are a few different ways to get a Custom 404 page using CodeIgniter, but one I like best is also the easiest to implement.

Simply place this code in a file called MY_Exceptions.php in your application/library folder (assuming your sub-class prefix is “MY_” set in config).

<?php
class MY_Exceptions extends CI_Exceptions{
    function MY_Exceptions(){
        parent::CI_Exceptions();
    }

    function show_404($page=''){

        $this->config =& get_config();
        $base_url = $this->config['base_url'];

        // could redirect to known controller - or redirect to home page
        header("location: ".$base_url);
        exit;
    }
}
?>

That’s it!  This example above is set to redirect 404 errors back to your home page.  You can easily change it to redirect visitors to a custom “Error” controller that you would build the same way as any other controller/view.

One Response to CodeIgniter Custom 404 Page

  1. Great Article! now how would you hand off to a controller? could you give an example of that?
    Thank you!

Leave a Reply