CodeIgniter 404 page
codeigniter, php
Solution
All the answers here seem very outdated (as of version 2, at least) or very overkill. Here's a much simpler and manageable way, which only involves two steps:
In application/config/routes.php modify the field 404_override to point to some controller:
// Specify some controller of your choosing
$route['404_override'] = 'MyCustom404Ctrl';
Inside the controller specified in your `routes.php`, implement the `index()` method so it loads your custom error 404 view. You might also want to set some headers so the browser knows you're returning a 404 error:
// Inside application/controllers/MyCustom404Ctrl.php
class MyCustom404Ctrl extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index(){
$this->output->set_status_header('404');
// Make sure you actually have some view file named 404.php
$this->load->view('404');
}
}
Like the in-code comment mentioned, be sure there's a view file in application/views/404.php where you actually have your custom 404 page.
As a side note, some of the answers in this page suggest you modify stuff inside /system, which is a bad idea because when you update your version of CodeIgniter, you'll override your changes. The solution I'm suggesting doesn't mess with core files, which makes your application much more portable and maintainable, and update resistant.
Problem
When I enter a non existent url on my site it takes me to the 404 page that is specified by this in routes.php: ``` $route['404_override'] = 'page/not_found'; ``` So the design of the page is as I have made it in the view not_found.php in the "page" directory. However if I use this function to manually enforce a 404: ``` show_404(); ``` It takes me to the default CodeIgniter 404 page with no style: ``` 404 Page Not Found The page you requested was not found. ``` How can I make it go to the same 404 page that I specified in the routes file?