How to Create Custom Codeigniter 404 Not Found Page

How to Create Custom Codeigniter 404 Not Found Page


404 Not Found is perhaps the most common type of error users encounter on the internet. It is one of the most common errors users experience during web browsing. Since the error could potentially impact the user experience, CMS and frameworks have come up with creative ways of handling the error.

Codeigniter offers a simple way of creating custom 404 error pages in which the website or the application could tackle the situation creatively and redirect the visitor to the appropriate page(s). This way, the developers have complete control over the user journey and could make sure that the visitor doesn’t bounce off the website. Codeigniter 404 Page Not Found error offers a great opportunity to retain user engagement and present related links and/or information about services or products to the visitors.

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.

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/ folderI have named this class My404 which has an index() method for loading the view.

  1. <?php
  2. class My404 extends CI_Controller
  3. {
  4. public function __construct()
  5. {
  6. parent::__construct();
  7. }
  8. public function index()
  9. {
  10. $this->output->set_status_header('404');
  11. $this->load->view('err404'); }
  12. }

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:

  1. $route['404_override'] = 'my404';

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.

  1. <?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
  2. <?php
  3. $ci = new CI_Controller();
  4. $ci =& get_instance();
  5. $ci->load->helper('url');
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="utf-8">
  11. <title>404 Page</title>
  12. </head>
  13. <body>
  14. <div>
  15. <p>How to Create Custom Codeigniter 404 Error Pages </p>
  16. <div>
  17. <p><a href="<?php echo base_url(); ?>">Go Back to Home</a></p>
  18. </div>
  19. </div>
  20. </body>
  21. </html>

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.

  1. <?php
  2. class Error extends Controller {
  3. function error_404()
  4. {
  5. $this->output->set_status_header('404');
  6. echo "404 - not found";
  7. }
  8. }

Now create MY_Router.php  in the system/application/libraries/ folder and add the following code to it:

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class MY_Router extends CI_Router {
  3. var $error_controller = 'error';
  4. var $error_method_404 = 'error_404';
  5. function My_Router()
  6. {
  7. parent::CI_Router();
  8. }
  9. function _validate_request($segments)
  10. {
  11. if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
  12. {
  13. return $segments;
  14. }
  15. if (is_dir(APPPATH.'controllers/'.$segments[0]))
  16. {
  17. $this->set_directory($segments[0]);
  18. $segments = array_slice($segments, 1);
  19. if (count($segments) > 0)
  20. {
  21. if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
  22. {
  23. return $this->error_404();
  24. }
  25. }
  26. else
  27. {
  28. $this->set_class($this->default_controller);
  29. $this->set_method('index');
  30. if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
  31. {
  32. $this->directory = '';
  33. return array();
  34. }
  35. }
  36. return $segments;
  37. }
  38. return $this->error_404();
  39. }
  40. function error_404()
  41. {
  42. $this->directory = "";
  43. $segments = array();
  44. $segments[] = $this->error_controller;
  45. $segments[] = $this->error_method_404;
  46. return $segments;
  47. }
  48. function fetch_class()
  49. {
  50. $this->check_method();
  51. return $this->class;
  52. }
  53. function check_method()
  54. {
  55. $ignore_remap = true;
  56. $class = $this->class;
  57. if (class_exists($class))
  58. {
  59. $class_methods = array_map('strtolower', get_class_methods($class));
  60. if($ignore_remap && in_array('_remap', $class_methods))
  61. {
  62. return;
  63. }
  64. if (! in_array(strtolower($this->method), $class_methods))
  65. {
  66. $this->directory = "";
  67. $this->class = $this->error_controller;
  68. $this->method = $this->error_method_404;
  69. include(APPPATH.'controllers/'.$this->fetch_directory().$this->error_controller.EXT);
  70. }
  71. }
  72. }
  73. function show_404()
  74. {
  75. include(APPPATH.'controllers/'.$this->fetch_directory().$this->error_controller.EXT);
  76. call_user_func(array($this->error_controller, $this->error_method_404));
  77. }
  78. }

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.

  1. $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.

Comments