Hi guys, this time I want to share a problem regarding CodeIgniter which found a lot of 404 error statuses because several pages had been deleted.
For SEO observers, of course this will be a big problem, because it affects SEO values. So can it be overcome? Of course it can, namely by customizing a special page to overcome 404, meaning that all pages that are not found will be directed to a special page that has been provided.
What is the Function of Custom Page 404 (not found)?
To try to keep users who end up on a 404 (didn't find what they were looking for) comfortable with your site, and start interacting with other alternatives.
Completion
There are many ways to do this in CodeIgniter, including using the CodeIgniter core's CI_Exeptions extends class.
Method 1 - CI_Exceptions
First make sure you enable the perfix setting in the config.php file (system/application/config/config.php)
$config['subclass_prefix'] = 'MY_';
Next, create a file named My_Exceptions.php in system/application/libraries. The last step is to override the show_404() function as follows.
class MY_Exceptions extends CI_Exceptions{
function MY_Exceptions(){
parent::CI_Exceptions();
}
function show_404($page=''){
$this->config =& get_config();
$base_url = $this->config['base_url'];
$_SESSION['error_message'] = 'Error message';
header("location: ".$base_url.'error.html');
exit;
}
}
Now every 404 error will display your own custom error message.
Method 2 - Controller
The first step is to add a route in the configuration.
$route['404_override'] = 'my404'; //my404 is class name.
Next, create a new class file in the controller and fill it with the code below. This code is very easy to understand, so don't worry.
<?php
class my404 extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->output->set_status_header('404');
$data['content'] = 'error_404'; // View name
$this->load->view('index',$data);//loading in my template
}
}
?>
Finally, create a new file in the view folder named "error_404.php" and feel free to customize the HTML display as you like.
Congratulations, problem solved!