The Right Way to Handle 404’s in Joomla

404: it’s probably the most known number in the Internet world, and it means that the page is not found. Ironically, 200, the response code that one will get when the page is found and everything’s OK, is not nearly as known. This, from our psychological perspective, sheds a light on the pessimistic nature of the Internet (half-full vs half-empty glass, or sort of…). But hey, we’re not psychologists, so let’s focus on what we know best: Joomla!

By default, when a page does not exist, Joomla displays a generic page with a red 404 – Not found header. Of course, if you are running a professional website, you need to change this page to something more elegant (e.g. making it use the same template as your website), and Joomla’s official documentation recommends you do so by following the below steps:

  1. Create an article called 404 in Joomla’s Article Manager. This article will contain your custom 404 error message. Note that the article can be called anything, but it’s better to call it 404.
  2. Create a menu item, also called 404 (again, it can be called anything – but calling it 404 makes it clear that this is your 404 menu item), which points to your 404 page. This means that going to http://www.yourjoomlawebsite.com/404.html will display your error page (assuming you have SEF turned on, and you’re using mod_rewrite, and you’re adding the .html suffix to your URLs).

  3. Copy the file error.php from the templates/system folder to the templates/[your-template-name] folder.

  4. Add the following code to the beginning of the copied error.php file (after restricted access):

    if (($this->error->getCode()) == '404') {
    	header('Location: http://www.yourjoomlawebsite.com/404.html');
    	exit;
    }

  5. That’s it!

Now, if you go to a page that doesn’t exist on your website, you will be automatically redirected to your fancy 404 page. We know, this is fascinating, but there is a problem…

You see, the above code redirects your non-existent pages to an existent, 404 page, using a 302 redirect. So, if you’re using Google Webmaster tools, non-existent pages will no longer show up in the Not Found tab on the Crawl Errors page, since you are telling Google (and all other search engines), that any non-existent page is temporarily redirected to your 404 page. This means that you won’t be able to fix all the problematic links to your content because you won’t be able to see them!

So, what is the right way to handle 404’s in Joomla?

The right way to do handle 404’s in Joomla would be to add the below code in the copied error.php file instead of the code above:

if (($this->error->getCode()) == '404') {
	header('HTTP/1.0 404 Not Found');
	$errorPageContent = file_get_contents('http://www.yourjoomlawebsite.com/404.html');
	echo $errorPageContent;
	exit;
}

The above code will not redirect to your 404 page, but instead, it will grab the content from your 404 page after telling Google (and, of course, other search engines) that the page is not found. This way, you will ensure that Google’s Webmaster Tools will still list all your non-found pages even after you change to the dull Joomla default 404 page to your new and improved 404 page!

We hope you found this post useful. If you need help with the implementation, then please contact us. We will do it for you in a heartbeat and for a really, really affordable fee!

One Response to “The Right Way to Handle 404’s in Joomla”
  1. Comment by Lonnie — February 8, 2017 @ 2:51 pm

    Very useful, I’ve hated Joomla’s instructions on using redirects.. I’ve adapted that tiny bit of code you have into a more complex system.

    I have it where it will query the menu table for a corresponding error code in the alias field and if it exists it pulls that in and displays it as you do, only I use a curl request since a few hosts don’t allow file_get_contents() to pull in http/https content.

    Since I’m using CURL I have it determine if it’s http/https and I also have it pulling the server name out of the $_SERVER array so it’s a drag and drop solution.

    All I do is drop error.php in the template folder, create the error page and create the menu.. if a menu item doesn’t exist it displays a generic error using an associative array containing all http error codes and descriptions. I know Joomla probably only passes along a couple of those error codes to error.php but I have it set up to handle all of them.. and all can have custom pages.

    No more infinite loops when one of our guys forgets to create the menu item!

Leave a comment