Aby Design : Custom Web Design and Development

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”

Author comments are in a darker gray color for you to easily identify the posts author in the comments

  1. Armon says:

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

Leave a Comment

Recently Delicious

Spreading the Word
© 2010 Aby Design. All Rights Reserved.
An company.