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.
Go to “application/config/routes.php” open and change below code.
create a controller with named “page404notfound.php” under “application/controllers/page404notfound.php” and add below code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page404notfound extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->output->set_status_header('404'); // setting header to 404
$this->load->view('page404notfound');//loading view
}
}
?>
create a view with named “page404notfound.php” under “application/views/page404notfound.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.
<?php
defined("BASEPATH") OR exit('no direct script access allowed');
class Controller_404 extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->output->set_status_header('404');
$this->load->view('err404');
}
}