HTTP status code for 404 – page not found in Joomla!

In case you are setting an error.php page in Joomla! to customise the 404 Page Not Found” error page, remember to add the following lines in your your template index.php page, before the DOCTYPE line:

if($this->error->code = '404'){
	header("HTTP/1.0 404 Not Found");
} ?>

The reason is that when a request is made for a page on your site, Joomla! returns the HTTP status code “200 – the server successfully returned the page” also for error pages. This is problematic for those working with Google Webmaster Tools and trying to get a sitemap resolved.

Information found (after weeks of search) here. That is: “When all else fails, read the instructions”.

UPDATE 12.8.2008: Forget what I have written above, at least in case you use SEF, it doesn’t work. That is, the if loop is inexplicably matched all the time, so it returns a 404 Page Not Found also for existing pages 🙁

UPDATE 27.8.2008: Following this suggestion on a Joomla! Forum thread, I managed to do the trick. The best way is to create the error.php page not in the way it is suggested on the Joomla! web site, but in the following way:

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

if (($this->error->code) == '404') {
  echo file_get_contents('http://www.yoursite.com/
                            error-404-page-not-found');
} 

where error-404-page-not-found is your customised “404 Page Not Found” error page. This will return a 404 page for any error it occurs. To prevent to show the 404 page for other errors, paste the full code showed in the post.

One comment

  1. Thank you, this fixed my IE problem!

    // no direct access
    defined( ‘_JEXEC’ ) or die( ‘Restricted access’ );

    if (($this->error->code) == ‘404’) {
    echo file_get_contents(‘http://www.yoursite.com/
    error-404-page-not-found’);
    }

Comments are closed.