Tags

, , , , ,

joomla_404

Today while working on Joomla 3.1 theming, I have encountered one situation, where I had to create a custom theme for the 404 error page of a website. Previously I have worked on this theming for Joomla 2.5 and older versions, but found no clues available for 3.x version.

Generally in older versions of Joomla, the process of creating a custom designed 404 error page is also the same for Joomla 3.x rather with a few coding changes. Here are the step that should be followed to create a custom designed 404 error page:

In the first step, let’s create an article and call it “404-page-not-found” or something to that effect. – Insert your desired text content for the article to match with other page layout designs.

Secondly, let’s add a menu item to one of the menus, preferably not your main menu or one of your other visible menus, as generally none of us want our site visitors to see a direct menu link to the 404 error page. Better option is to create a new menu for the error pages and call it “Error pages”. When we create the menu item, link it to the article you created in the first step, give it a title, and publish it.

The next step is to locate the default error.php file which creates the Joomla 404 error page. This file  is generally located at “templates/system/” directory. Copy the error.php file to your Templates directory. By doing this, we are instructing joomla to refer to the error.php template file to show the error pages.

The last step involves editing the error.php file. Let’s open the file with a preferred text editor. (I always prefer Notepad++).

Now search for the code “defined( ‘_JEXEC’ ) or die( ‘Restricted access’ );” in the error.php file and change it to this:

defined( '_JEXEC' ) or die( 'Restricted access' );
if (($this->error->code) == '404') {
header('Location: http://yourdomain.com/404-page-not-found);
exit;
}

Now don’t forget to change the “Location:” URL in the code to the menu item we have created earlier.

Hope you get your new 404 error file ready to rock. But wait, here I will add a simple change in code which is needed to work for Joomla 3.x.

Edit the error.php file and add the following lines of code right under
defined(‘_JEXEC’) or die;

if (($this->error->getCode()) == '404') {
header('Location: /index.php/404-page-not-found');
exit;
}

Now here also don’t forget to change the “Location:” URL in the code to the menu item we have created earlier.

Truly this has helped me a lot in solving my problem with joomla 3.x and hope it will help you as well.

Do drop me a comment if you need any help or if you want to say a thanks 🙂

Advertisements