It’s easy to handle 404 error in codeigniter or we can create a custom 404 page in codeigniter. we just need our own controller and a view for this. some of url that doesn’t found our website, some broken links, typing incorrect URL goes to 404 – page not found error. so if we need to show our own custom page for this we need to create custom error pages in codeigniter. Let’s see how to create custom 404 page in codeigniter :-
1. Go to “application/config/routes.php” open and change below code.
$route['404_override'] = 'pagenotfound'; // adding custom controller.
2. create a controller with named “pagenotfound.php” under “application/controllers/pagenotfound.php” and add below code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pagenotfound extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->output->set_status_header('404'); // setting header to 404
$this->load->view('pagenotfound');//loading view
}
}
?>
3. create a view with named “pagenotfound.php” under “application/views/pagenotfound.php” and add below code (or add code to show on 404 page).
<html>
<body>
<section>
<div>
<div align="center" style="margin-top:50px; margin-bottom:50px;">
<img src="<?php echo base_url();?>images/404.jpg" /> // will be your image path.
</div>
</div>
</section>
</body>
</html>
Now type any wrong url on your site and see you have all set with your custom 404 error page in codeigniter.