Skip to main content

A Better Joomla 404 Error
08 June 2013

We humans recognize a 404 error as a page not found. That is helpful nevertheless the foremost reason of a 404 error code is to let the requesting computer know that the page was not found. Most of the online Joomla 404 error tutorials are missing the point and giving developers a method that is returning a 200 OK code.

Here is How to Set up a 404 with Joomla

First, create an article. The name of the article would benefit from being called 'Page Not Found' or something similar. Within the article's content tell your users what went wrong. Hit save and make a note of the article's ID number.

Then FTP into your theme folder's root and add a new file called 'error.php'. The 'error.php' file is an override that is called by Joomla automatically if a requested webpage cannot be found. Once you have added the 'error.php' page, paste in the first template you can find below. Swap out the placeholders.

<?php
defined( '_JEXEC' ) or die('Restricted access');
if (($this->error->getCode()) == '404') {
	echo file_get_contents('http://YOUR_DOMAIN/index.php?option=com_content&view=article&id=ARTICLE_ID');
}

This solution pulls in the content of the 'Page Not Found'. In fact it goes off and gets the whole page in its own request. Don't worry about the hideous looking URL - the users will never see it. Doing it this way will send the 404 error code back as well as the human readable error page your graphic designer crafted. FYI Joomla sets the 404 code in the header.

How Not to Set up a 404 with Joomla

Most online tutorials show a redirect method similar to:

<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
header("HTTP/1.0 404 Not Found");
header("Location: http://YOUR_DOMAIN/404");
exit;

The problem with this is the redirect will resolve with a 200 OK code. A 200 OK code tells the browser that the request was all ok. That is not good for Google! You need to tell Google that the page could not be found too.

Mailing List

Responsive Media

With the ResponsiveMedia plugin for Joomla it is easy to add 3rd party content from YouTube, Vimeo, and Instagram right in to any Joomla! article.

ResponsiveMedia