How can I redirect a 404 Error in a custom 404 page using Codeigniter?

there is another way which I use: by overriding Exception core class of Codeigniter. Firstly make sure your config file(system/application/config/config.php) subclass prefix is as following
$config['subclass_prefix'] = 'MY_';

Then make a file named MY_Exceptions.php in system/application/libraries. Then override the function show_404() function here as follows.

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'];

        $_SESSION['error_message'] = 'Error message';
        header("location: ".$base_url.'error.html');
        exit;
    }
}

Now Error controller will be your error page, where the page will be redirected for 404 error.

Leave a Comment