There are plenty of resons for having a 404 page even though you are sure that you don't have any broken link on your website. Because you cannot avoid user typing incorrect URL pointing to a page on your domain which doesn't exists. Also some websites can link to some URL on your website which no longer valid. When there is no 404 page user is presented with the default 404 page which is set by the hosting provider which changes the impression about your website.
CodeIgniter provides an easy way to build a custom 404 (Page-not-found) page. Here is the step by step on setting up a 404 page for your CodeIgniter website.
First open routes.php in the application/config folder and set a custom controller name.
$route['404_override'] = 'my404'; //my404 is class name.
Create a new controller and write the following code in it. The code is very easy to understand. So I am not going to explain it here.
<?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
}
}
?>
Create a view file with name 'error_404.php' with your custom message.
That is all you need to do and will be having your custom 404 page now.
Do you know that you can generate a complete PHP websites in minutes with PHP Website Generator?