RSS

Search Engine Optimization & Performance tuning for your Joomla website

Web Hosting

Avoid 404-errors

Want to support this site? Buy my E-book: Joomla 3.5 SEO & Performance!

Make sure that people can never end up with a meaningless 404 Page not found error message. Especially the default Joomla page for this is very ugly. As it will also completely different from the rest of your site, people will think the site is broken and leave it. If this happens often it can negatively impact your SEO rankings. Best is to create a custom 404-page for your template. This works a bit differently in Joomla 2.5 or Joomla 3:

Create a custom 404-page in Joomla 2.5

There is quite an easy way to create a custom 404 page in Joomla 2.5. First, just create an article for this purpose. Call it whatever you want (404, error page, etc.) and just type a nice text, plus links to your Home or Search page. Then add it to a menu, but leave the menu-item unpublished.

Then just copy file templates/system/error.php to your current template's root folder.

For Joomla 2.5: look up the following section:

defined('_JEXEC') or die;

And directly after this, add this section:

if (($this->error->getCode()) == '404') {
header('Location: /index.php?option=com_content&view=article&id=999');
exit;
}

Replace the article id (999 in this case) by the article ID of your own article.

You can also download the file here (so put it in your template folder and replace the article's ID):

error.php (2.5 version, zipped)

Create a custom 404-page in Joomla 3

Custom 404 pages in Joomla 3 can be created equally easy. Again, create an article for this purpose. Call it whatever you want (404, error page, etc.) and just type a nice text, plus links to your Home or Search page. Then add it to a menu, but leave the menu-item unpublished.

Then just copy file templates/system/error.php to your current template's root folder.

Look up the following section:

defined('_JEXEC') or die;
if (!isset($this->error))
{
$this->error = JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR'));
$this->debug = false;
}
//get language and direction
$doc = JFactory::getDocument();
$this->language = $doc->language;
$this->direction = $doc->direction;

?>

Now, add the complete following section to the code, just after the last line, before the closing PHP tag, so it look like this:

defined('_JEXEC') or die;
if (!isset($this->error))
{
$this->error = JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR'));
$this->debug = false;
}
//get language and direction
$doc = JFactory::getDocument();
$this->language = $doc->language;
$this->direction = $doc->direction;

if (($this->error->getCode()) == '404') {
header('Location: /index.php?option=com_content&view=article&id=999');
exit;
}

?>

Replace the article id (999 in this case) by the article ID of your own article.

Download the file here (put it in your template folder and replace the article's ID):

error.php (Joomla 3 version, zipped)

Improved code

An alternative which might even be better than both examples above is to replace the line starting with header("Location.... with:

header("HTTP/1.0 404 Not Found");
echo file_get_contents(JURI::root().'/index.php?option=com_content&view=article&id=999');

Note that on some hosts the slash before index.php? needs to be removed to prevent errors.

What this does is to send a 404 HTTP header to the browser, stay on the same URL, and retrieve the content of the 404 page, instead of just going there.

Avoid 404 errors

Of course you should try to avoid 404 errors in the first place. Therefore it is important to have a readable URL structure that helps people to easily navigate it. So, say if you a URL like this: /webshop/products/product1, sometimes people tend to just remove the last word because they want to see all products. So, make sure this page actually exists.

The second reason that people can end up on your 404 page is because they used an old link. In this case you should redirect them, using a 301-redirect. This is explained an a separate article.

Like this article?    Share it!