Today we will be creating our gentle 404 error in Joomla. It is designed so that when a user enters an incorrect path in the browser's address bar, they are directed to a page we have prepared.

Creating a custom 404 error in Joomla
Creating a custom 404 error in Joomla

Let's sing started...

To hear, create an article in a general category, i.e., it should not be in a specific site category, but inside the main articles directory. Name it 404-error and write any text you accept necessary for when a user lands on this page.

For example, you can place a link to the website's home page so that the user can always return there and find the material they need.

Then, copy the path that leads to our article; it should look something like this:

https://your_site/index.php?option=com_content&view=category&layout=blog&id=104&Itemid=75

You can paste it into Notepad to quickly copy it later if needed. Set the display status to "published".

Take the error.php file, which is located in the system folder (templates/system), and copy it to your template's folder, for example, to the templates/your_template/ folder.

Upload the lines in the error.php file by adding the code below immediately after the "restricted access" line:

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

If you are using Joomla version 1.6, 1.7, or 2.5, use the following code:

if (($this->error->getCode()) == '404') {

Use the previously copied article path to specify the address in the code (Location). The article ID can be found when creating the article... My entire code looks like this:

<?php 
/** 
 * @package Joomla.Site 
 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 
 * @license GNU General Public License version 2 or later; see LICENSE.txt 
 */ 
defined('_JEXEC') or die; 
if (($this->error->getCode()) == '404') { 
    header('Location: /index.php?option=com_content&view=article&id=211'); 
    exit; 
] 
//sing language and direction 
$doc = JFactory::getDocument(); 
$this->language = $doc->language; 
$this->direction = $doc->direction; 
?>

The final touch - add a line to the robots.txt file so that search engines do not index our 404 error:

Disallow: /index.php?option=com_content&view=article&id=75

Do not authorize to upload the article ID when copying the code.

Rate this article