How to create a custom 404 page in Codeigniter

 In this tutorial we will be learning how to create a custom '404 page' or 'Page not found' in Codeigniter. Here we will redirect 404 page to existing page in the following steps. You can create a custom page instead of redirecting to existing one.

 


1. Go to "application/config/routes.php"

    Now search for  $route['404_override'] = ' '; Replace it with the following piece of            code.

    $route['404_override'] = 'Custom404';

 

2. Now create a new controller "Custom404.php" with the following code. 

  <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Custom404 extends CI_Controller {

  public function __construct() {

    parent::__construct();

    // load base_url
    $this->load->helper('url');
  }

  public function index(){
 
    $this->output->set_status_header('404');
    $this->load->view('errorPage');
 
  }

}
?>

3. Now create a new view for 404 page name by "errorPage" with your styling

Alternatively you can redirect the same to existing page by replacing "errorPage"     with "existingPage".

Previous
Next Post »
Related Posts Plugin for WordPress, Blogger...