CodeIgniter Create a Custom Error 404 Page

Installing CodeIgniter

If you don't have CodeIgniter installed yet, you can use this link to download the latest version of CodeIgniter which is 3.1.7 that I've used in this tutorial. After downloading, extract the file in the folder of your server. Since I'm using XAMPP as my localhost server, I've put the folder in htdocs folder of my XAMPP. Then, you can test whether you have successfully installed codeigniter by typing your app name in your browser. In my case, I named my app as codeigniter_404 so I'm using the below code.
  1. localhost/codeigniter_404
codeigniter successful install

Adding Bootstrap

In this tutorial, I'll be using bootstrap which is included in the downloadable of this tutorial but if you want, you may download bootstrap using this link.

Creating our .htaccess

Next, we need to create .htaccess page to enable codeigniter's error 404 page. In your app's root folder, create a file named .htaccess and add the ff codes:
  1. <IfModule mod_rewrite.c>
  2.         RewriteEngine On
  3.         RewriteCond %{REQUEST_FILENAME} !-f
  4.         RewriteCond %{REQUEST_FILENAME} !-d
  5.         RewriteRule ^(.*)$ index.php/$1 [L]
  6. </IfModule>

Configuring our App

Next, we configure our base url and removing index.php in our index_page to remove it to our URLs. Open config.php located in application/config folder and edit the following lines.
  1. $config['base_url'] = 'http://localhost/codeigniter_404/';
  2. $config['index_page'] = '';

Creating our 404 Page

Lastly, we create our custom 404 page by updating the contents of error_404.php located in application/views/error/html folder by the ff codes.
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. ?>
  4. <?php
  5.         $ci = new CI_Controller();
  6.         $ci =& get_instance();
  7.         $ci->load->helper('url');
  8. ?>
  9. <!DOCTYPE html>
  10. <html>
  11. <head>
  12.         <meta charset="utf-8">
  13.         <title>404 Error Page not Found</title>
  14.         <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
  15.         <style type="text/css">
  16.                 .errorDiv{
  17.                         padding: 40px 15px;
  18.                         text-align: center;
  19.                 }
  20.                 .errorMsg{
  21.                         margin-top:15px;margin-bottom:15px;
  22.                 }
  23.                 .errorBtn .btn{
  24.                         margin-right:10px;
  25.                 }
  26.         </style>
  27. </head>
  28. <body>
  29. <div class="container">
  30.     <div class="row">
  31.         <div class="col-md-12">
  32.             <div class="errorDiv">
  33.                 <h1>
  34.                     Oops!</h1>
  35.                 <h2>
  36.                     404 Not Found</h2>
  37.                 <div class="errorMsg">
  38.                     Sorry, an error has occurred. Requested page not found!
  39.                 </div>
  40.                 <div class="errorBtn">
  41.                     <a href="<?php echo base_url() ?>welcome/" class="btn btn-primary btn-lg"><span class="glyphicon glyphicon-home"></span>
  42.                         Take Me Home </a><a href="#" class="btn btn-default btn-lg"><span class="glyphicon glyphicon-envelope"></span> Contact Support </a>
  43.                 </div>
  44.             </div>
  45.         </div>
  46.     </div>
  47. </div>
  48. </body>
  49. </html>
In here, we have created a new controller on the page to access our base_url. That ends this tutorial. Happy Coding :)

Add new comment