How to create custom 404 error page?

404 not found error page.

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.

$route['404_override'] = 'page404notfound'; // adding custom controller.

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.

Route

File Name :

$route['default_controller'] = 'home';
$route['404_override'] = 'controller_404';
$route['translate_uri_dashes'] = FALSE;

Contorller

File Name :

<?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');
}
}


View

File Name : err404.php

<html>
<head>

</head>
<body>

<img src="<?php echo base_url()?>assets/images/error_404.png" width="800" height="600" alt="404 page"/>
<br/>
<div>
<center><a href='<?= base_url(); ?>' >Back to Homepage</a></center>
</div>
</body>
</html>





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here