making a custom fully working 404 page with codeigniter

[update: I changed this to use exceptions, it’s better this way]

CodeIgniter has a really annoying bug with its 404_override setting which makes it pretty much useless as a complete solution. The bug is that it won’t load the correct libraries and helpers of your 404 controller if the controller which threw the 404 also loaded those modules for itself. Fear not, a workaround is at hand. The advantage of properly handling a 404 in the context of your CodeIgniter application is that you get access to your standard stuff like your site layout (views) so you can load all your menus and stuff and load the error message where the content would normally go – this way the visitor feels like they’re on the site and they can get onto a working page easily, if they so desire. You can also hook into your database and perhaps do some logging.

Firstly, if you’re not using MY_Controller then, err, well you really should be. It makes life so much easier. MY_Controller is an easy to define superclass for all of your controllers. Implementing it lets you centralise all your site-wide constants and routines, and it’s very useful here.

Define it in application/core/MY_Controller.php and make all your ‘real’ controllers extend it. Also define individual exception classes that you want to handle. The _remap method wraps the controller’s method invocation, so we override that and use it to catch exceptions. This means that at any point in your controllers, you can just throw an Exception404 and you don’t have to worry about anything else; the exception bubbles up to _remap which unloads any existing output and then loads the 404 view(s).

You could extend this to also define 403 exceptions, and any other error codes your application may deal with, and you could use the exception message (i.e. throw new Exception404(‘some message’);) to set some descriptive text for your 404 view. [update: actually I tried this and it’s a bit cumbersome, you’re probably best off having just a HTTPException class and set the status code as a constructor argument)]

This is sort of how mine looks:

// defined for neater syntax only
class Exception404 extends Exception {

public class MY_Controller extends CI_Controller {
  public function _remap($method, $params = array()) {
    try {
      if (!method_exists($this, $method))
        throw new Exception404();
      return call_user_func_array(array($this, $method), $params);
    } catch(Exception404 $e) {
  protected function _load_header($data=array()) {
    $this->load->view('headerview.php', $data);
  protected function _load_footer() {
  protected function show_404() {
    // clear any views that have already been loaded

CodeIgniter URLs work like this:


So we can see here that when we request a method of a controller, the remap function catches it. If the URL is okay and the method exists, it is executed. If it doesn’t, the 404 is executed.

To make it so it also catches non-existent controllers, change application/config/routes.php:

$route['404_override'] = 'CONTROLLER/show_404';

CONTROLLER can be any controller which inherits MY_Controller. I use my default, you can use whatever most appeals or, if you want, define a new controller.

You may (probably) have your application and server set up so it’s possible to request URLs which aren’t handled by CodeIgniter’s index.php. In this case, you probably also want to redirect when Apache catches a 404 to your new custom 404. Put this in your htaccess:

ErrorDocument 404 /index.php/CONTROLLER/show_404

Where again, CONTROLLER is some controller that implements show404.

you should now have site-wide redirection of file-not-found URLs to your exciting and sexy new 404 page. I thoroughly recommend using the opportunity to display to your baffled visitor a picture of a lolcat. I recommend this one:


I like blogging

Tagged with: , ,
Posted in Uncategorized
One comment on “making a custom fully working 404 page with codeigniter
  1. chadnoo says:

    I am using CI 2.2 version.

    **First approach**

    I changed the config/routes.php to redirect 404 error page to my custom made 404 page.

    $route[‘404_override’] = ‘custom/custom/custom404’; //custom folder/custom controller/method in custom controller.

    The router didn’t redirect to my custom controller.

    **Second approach**

    $route[‘404_override’] = ”; //changed routes

    $config[‘subclass_prefix’] = ‘MY_’; //changed routes

    //added new file


    class MY_Exception extends CI_Exceptions{

    function MY_Exceptions(){

    public function show_404($page=”)
    $CI =& get_instance();
    echo $CI->output->get_output();


    still I am getting the default CI page. I am not getting my custom 404 page. Please help me.

    My controller are kept in individual folders for example.
    For employe details and reports.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: