This post is on creating a custom 404 page in CodeIgniter.
Why you need a custom 404 error page
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.
How to setup a 404 page in CodeIgniter 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.
class my404 extends CI_Controller
public function __construct()
public function index()
$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.
Best Practises for 404 pages
- Always send the status header as 404 for the search engines will know the pages are 404 pages and should not be indexed.
- Provide a search access to your website so that you can easily find what they are looking for in your website.
- Keep it simple and you need to always apologies for missing the page, even if use has typed some incorrect address.
- Appearance: It should look like the same as rest of your site. If someone appears on a page that has none of the branding or functionality that they were expecting, they are far more likely to close the window and go back to Google. Keep them engaged by keeping your branding and appearance consistent.
- Content: Just like any other page on your site, the tone and function of your written copy is crucial if you are going to get your visitors to take the desired action. Make sure your language is welcoming and re-assuring.
- Help Users Out: Make sure you tell your visitors what you want them to do next! Give them a call to action. Help them find what they were looking for by either providing the most relevant links or by giving them an option to search within your site. Or, simply give them a positive experience by making them laugh or give them a game to play.