How to create custom 404 page with Codeigniter

Written by msnisha

Topics: Codeigniter

Broken links will give a bad impression to your users. So always do an analysis on your website using some tools if there is any broken link in the navigation links. But you can not fix the broken links from external websites to your website. Also some user might type a url to your website with spelling mistake. So it is always nice to have a good Page-not-found page to keep a good impression. You can implement a Page-not-found page in easy 3 steps given below.

  1. First open routes.php in application/config and set a custom controller name.
    $route['404_override'] = 'my404'; //Here my404 is my controller class name.
  2. Create a new controller and write the following code in it. The code is very easy to understand. So I am not going to explain it here.
    <?php
    class my404 extends CI_Controller
    {
    	public function __construct()
    	{
    	        parent::__construct();
    	}
    
    	public function index()
    	{
    		$this->output->set_status_header('404');
    		$data['content'] = 'error_404'; // View name
    		$this->load->view('index',$data);//loading in my template
    	}
    }
    ?>
  3. Create a view file with name ‘error_404.php’ with your custom message.

That is all. :)

6 Comments For This Post I'd Love to Hear Yours!

  1. TagTraday says:

    very interesting, thanks

  2. Ari says:

    Sorry, it works, but only for non-controllers. If you want to enter to something like that:
    http://www.url.com/existing-controller/non-method
    it doesn’t work

  3. Louis says:

    After making the hlloowerld.php and index_view.php files when i test it in the brower it gives the following error : Fatal error: Class Controller’ not found in C:\xampp\htdocs\CodeIgniter\application\controllers\helloworld.php on line 2 Since i am new to codeigniter plz help me with this issue.

  4. irfan says:

    instresting

  5. msnisha says:

    Do you have a controller with the default controller name? Try removing the underscore in the controller name.

  6. siddappa says:

    working fine

Leave a Comment Here's Your Chance to Be Heard!