Redirect all 404 error pages to custom 404 page Codeigniter HMVC

Viewed 1272

I've created a custom 404 not-found method in my home controller

I've set `$route['404_override'] = 'home/notfound';

and it works well for not found url's right after the main domain (e.g. www.domain.com/uroetiuosyerit)

but if i type in a class name then a invalid url (e.g. www.domain.com/services/yersityijftnyutyum) it's showing the default codeigniter 404 content

I'm using codigniter 3 with hmvc modeler extension https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

2 Answers

Create controller like My404 in application/controlers folder and add follow code:

<?php defined ('BASEPATH') or exit ('No direct script acces allowed');

class My404 extends CI_Controler {

  public function __construct()
  {
  parent::__construct();
  }

  public function index()
  {
  $this->output->set_status_header('404');
  $this->load->view('err404');
  }

}

Next, set up the route in the application/config/routes.php file:

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

Now, you create view in application/view folder and add follow code:

<?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>

or you can customize from your theme or template.

Related