Setup custom 404 page in codeigniter

setup custom 404 page in codeigniter

As a web application developer, It is always a very great practice to handle Page not found or 404 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 few lines of 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.

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 –

<p>
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:
</p> 
<ul class="error-page"> 
<li> If you typed in a URL, check that it is typed in correctly.</li>           
<li> Go to 
    <a href="http://www.coolajax.net" title="" class="global_link">www.coolajax.net</a> 
    and try to find links to where you want to go.
</li>
<li> Perhaps it was just a fluke, and if you try again by clicking refresh, it'll pop right up!</li>            
<li> Maybe the link is permanently deleted!</li>

</ul>

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!

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*

Back To Top