Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I redirect a 404 Error in a custom 404 page using Codeigniter?

Tags:

Kind sirs, I'm using Codeigniter to build a blog. I might need a way to redirect a 404 error into a custom 404 page. Just like what Abduzeedo.com's 404 page. Is it possible to control this by using routes? Or should i use controllers to direct it to another view? Thanks very much!

like image 233
Chester Sabado Avatar asked Feb 22 '10 09:02

Chester Sabado


People also ask

How do I trigger a 404 error?

One typical trigger for an error 404 message is when the page has been deleted from the website. The page was moved to another URL and the redirection was done incorrectly. You entered an incorrect URL address. Although it happens very rarely, sometimes the server malfunctions.


1 Answers

there is another way which I use: by overriding Exception core class of Codeigniter. Firstly make sure your config file(system/application/config/config.php) subclass prefix is as following $config['subclass_prefix'] = 'MY_';

Then make a file named MY_Exceptions.php in system/application/libraries. Then override the function show_404() function here 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 Error controller will be your error page, where the page will be redirected for 404 error.

like image 145
sumanchalki Avatar answered Oct 06 '22 01:10

sumanchalki