Codeigniter & PHP - forcing a 404?
show_404()
actually sends the proper headers for a search engine to register it as a 404 page (it sends 404 status).
Use a Firefox addon to check the headers received when calling show_404()
. You will see it sends the proper HTTP Status Code.
Check the default application/errors/error_404.php
. The first line is:
<?php header("HTTP/1.1 404 Not Found"); ?>
That line sets the HTTP Status as 404. It's all you need for the search engine to read your page as a 404 page.
If you want a custom error page you can do the following thing.In your Libraries create a file name MY_Exceptions and extend it with CI_Exceptions.And then override the show_404() function.In this function you can now create an instance of your Controller class using &get_instance() function.And using this instance you can load your custom 404 Error page.
class MY_Exceptions extends CI_Exceptions {public function __construct(){ parent::__construct();}function show_404($page = ''){ // error page logic header("HTTP/1.1 404 Not Found"); $heading = "404 Page Not Found"; $message = "The page you requested was not found "; $CI =& get_instance(); $CI->load->view('/*Name of you custom 404 error page.*/');}