Key Takeaways
- The CodeIgniter 404 Page Not Found error occurs when a resource is missing or a link is broken.
- You can create a custom 404 controller (My404) and load a user-friendly view.
- Update routes.php with $route[‘404_override’] to use your custom error page.
- A custom 404 page helps improve UX, reduce bounce rate, and retain visitors.
404 Not Found is one of the most common errors users face online. In CodeIgniter, the 404 Page Not Found error can be customized to enhance user experience and keep visitors engaged instead of bouncing. By creating a custom error page, developers can redirect users, add helpful links, or show relevant content. In this tutorial, you’ll learn how to create a fully customized CodeIgniter 404 page using controllers, routes, and views.
Why 404 Not Found Error Occurs
404 Not Found is the HTML error code that indicates a particular situation in which the connection between the server and the client is working correctly, but the server could not find the resource requested by the client.
The underlying causes of the error are several, but the general process is similar in all cases. The server has received a request for a resource that is non-existent on the server of PHP hosting. A very common cause of this situation is the broken links that point to pages or resources that have been removed (or taken down) by the website administrator.
In this article, I will show you how you can create a fully customized 404 error page in Codeigniter. I will start by creating the controller and then move on to set up the routes.
You might also like: Use Elasticsearch in Codeigniter
Create the Controller for Custom Codeigniter 404 Page Not Found Error
I will start by creating a class for the controller in the application/controllers/ folder . I have named this class My404 which has an index() method for loading the view.
<?php
class My404 extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->output->set_status_header('404');
$this->load->view('err404'); }
}
You might also like: Pass Data From Controller to View in CodeIgniter
Set up the Route
Next, I will set up the route in the application/config/routes.php file. This file will contain the following line that overrides the default route:
$route['404_override'] = 'my404';
Stop Losing Visitors to 404 Errors
With Cloudways managed hosting, you get faster load times, robust security, and seamless app management – so you can focus on building great CodeIgniter apps.
Creating the View
I will now describe the view of the custom 404 page in the application/views folder. you will notice that in the following complete code of the view that I have redirected the visitor to the homepage.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php
$ci = new CI_Controller();
$ci =& get_instance();
$ci->load->helper('url');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>404 Page</title>
</head>
<body>
<div>
<p>How to Create Custom Codeigniter 404 Error Pages </p>
<div>
<p><a href="<?php echo base_url(); ?>">Go Back to Home</a></p>
</div>
</div>
</body>
</html>
You might also like: Create Multiple Database Connections in CodeIgniter
Alternative Method of Creating the Controller
Another (and much simpler) way of creating a custom 404 page for your Codeigniter project is to add the following code to the system/application/controllers/error.php file. In this code, I have extended the controller class into a custom class named Error. This class contains the method error_404() that outputs the “404 – Not Found” error if it encounters the 404 code in the header.
<?php
class Error extends Controller {
function error_404()
{
$this->output->set_status_header('404');
echo "404 - not found";
}
}
Now create MY_Router.php in the system/application/libraries/ folder and add the following code to it:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {
var $error_controller = 'error';
var $error_method_404 = 'error_404';
function My_Router()
{
parent::CI_Router();
}
function _validate_request($segments)
{
if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
{
return $segments;
}
if (is_dir(APPPATH.'controllers/'.$segments[0]))
{
$this->set_directory($segments[0]);
$segments = array_slice($segments, 1);
if (count($segments) > 0)
{
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
{
return $this->error_404();
}
}
else
{
$this->set_class($this->default_controller);
$this->set_method('index');
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
{
$this->directory = '';
return array();
}
}
return $segments;
}
return $this->error_404();
}
function error_404()
{
$this->directory = "";
$segments = array();
$segments[] = $this->error_controller;
$segments[] = $this->error_method_404;
return $segments;
}
function fetch_class()
{
$this->check_method();
return $this->class;
}
function check_method()
{
$ignore_remap = true;
$class = $this->class;
if (class_exists($class))
{
$class_methods = array_map('strtolower', get_class_methods($class));
if($ignore_remap && in_array('_remap', $class_methods))
{
return;
}
if (! in_array(strtolower($this->method), $class_methods))
{
$this->directory = "";
$this->class = $this->error_controller;
$this->method = $this->error_method_404;
include(APPPATH.'controllers/'.$this->fetch_directory().$this->error_controller.EXT);
}
}
}
function show_404()
{
include(APPPATH.'controllers/'.$this->fetch_directory().$this->error_controller.EXT);
call_user_func(array($this->error_controller, $this->error_method_404));
}
}
Finally, I will call the 404 Page manually by calling the show_404() method that checks the two parameters passed in its array and shows error if it discovers that a parameter is missing. Use the following statement below to manually call the 404 Page.
$this->router->show_404();
Conclusion
In this short tutorial, I have demonstrated two methods that you can use for creating (and calling) a custom designed 404 error page for your Codeigniter application. A custom 404 error page is an ideal way of retaining visitors on the website and redirecting them to appropriate pages on the website. This greatly reduces the bounce rate of the websites and ensures that the visitors remain interested in your website. If you have further questions and queries about creating custom 404 pages for Codeigniter projects, do post them in the comments below.
Frequently Asked Questions
Q1: What is CodeIgniter 404 Page Not Found?
It’s an error shown when a requested resource or page is missing from a CodeIgniter application.
Q2: How do I create a custom 404 page in CodeIgniter?
You can create a new controller (e.g., My404), add a custom view, and set $route[‘404_override’] in routes.php.
Q3: Can I redirect users from the 404 page in CodeIgniter?
Yes, you can add redirect links in the custom view to guide users back to the homepage or important pages.
Q4: Why should I use a custom 404 page in CodeIgniter?
It improves user experience, reduces bounce rate, and allows you to present helpful links or information instead of a blank error.
Owais Khan
Owais works as a Marketing Manager at Cloudways (managed hosting platform) where he focuses on growth, demand generation, and strategic partnerships. With more than a decade of experience in digital marketing and B2B, Owais prefers to build systems that help teams achieve their full potential.