Monday 30 January 2012

How to change the awful Joomla 404 page!

So, you have a Joomla site, and you constantly add and re-arrange stuff around, like renaming menu-items, aliases and turning on or off the SEF configuration.

Here's a great article that helps us do that.
http://www.hotjoomlatemplates.com/blog/joomla-template-tutorial/28-joomla-404-component-not-found-page-change

But what if I want to take one more step ahead of that? You might not want users to see a 404 page, even if that is a nice and pretty customized one.

You can add the following in the file %joomla_site_root%/templates/system/error.php ,
if you believe you can handle the PHP coding:

<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
if (($this->error->code) == '404') {
  $url = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
  // Here is the part where we can distinguish what was the user's request about.
  if (strpos($url, 'content') != false) {
    // The user was probably looking for an article
    header("Location: http://yoursite.com/url_for_your_main_list_of_articles");
  }
  else if (strpos($url, 'contact') != false) {
    header("Location: http://yoursite.com/contact_page");
  }
}
?>

Depending on your content, and knowledge of your previous URLs that people might click, you can also check for the whole URL like
if ($url == 'http://yoursite.com/index.php?option=com_content&view=article&id=55') {
  header("Location: http://yoursite.com/new_nice_sef_url_for_that_article");
}

Warning: You should always keep a backup before editing a joomla core file. Keep a backup before you start, and a backup of the edited file, after you are finished. This way, you can restore the original if you fail to do something, or restore the edited if Joomla replaces your work in an update.

No comments: