setup custom 404 page in codeigniter

  July 01,2012   Read: 840 Times

As a web developer, It is always a very great practice to handle Page-not-found issue and create a custom 404 page to keep a good impression of your website though the link is broken. If you have a nice 404 page in your website then you can show custom message in their why link is broken? or few sweet apology words for that inconvenience.

In codeigniter, We can easily setup a nice custom 404 page with just 3 steps.

Hope You may also like related 404 Post:

Best 404 Pages examples for web designers

Step 01: Customize routes.php

Go to application/config/routes.php. Open routes.php and add following code below "default_controller" routes statement.

$route['default_controller'] = "page";
$route['404_override'] = 'not_found'; 

Step 02: Create a not_found controller

When our application failed to open a link then we need to redirect user a custom 404 controller. For our application we create a controller called "not_found" and add following code in that controller.

class Not_found extends CI_Controller {

    public function __construct() {
            parent::__construct();              
    }

    //
    // @ start 404 page code
    //
    public function index() {

        $this->load->view("404_error_page.php", $data);


    }

}

Step 03: Create a user friendly 404_error_page.php page

As i told starting of the tutorial we need keep user in our website and that's why we add some user friendly 404 text in our view file. I've add some user friendly 404 page text as example. You can change them according to your website context. Have a look -

Opss! I'm sorry! This is quite embarrassing, I must say, but I can't find the web page you are looking for. Probably just misplaced it, but who knows? What should you do at this point? Well, you have a few options:

  • If you typed in a URL, check that it is typed in correctly.
  • Go to www.coolajax.net and try to find links to where you want to go.
  • Perhaps it was just a fluke, and if you try again by clicking refresh, it'll pop right up!
  • Maybe the link is permanently deleted!

Demo

Was this information useful? What other tips would you like to read about in the future?  Share your comments, feedback and experiences with us by commenting below!

  You might also like...

  codeigniter tips how to make url seo friendly

Share:

  Author: Md Mahbub Alam Khan (255 Posts)

Md Mahbub Alam Khan is the Editor and Founder of CoolAJAX Web Blog. You can follow CoolAJAX on Twitter , on Facebook or you can subscribe via RSS .

Write For Us & Be An Exclusive Post Author!

No Next Post Available! Previous tutorial 
comments powered by Disqus