Jan 3, 2012 at 11:35am PST by Mike Pearl
C'mon guys, the default 404 error page in Joomla is... well, it sucks... just awful! If you haven't yet taken a look, go to http://yoursite.com/name-a-page-that-doesnt-exist.html. That same old ugly page that tells visitors, "Go away you moron!" Why would you want to insult your visitors that way, just because they mistyped the url. Well, there's a very simple way to fix this.
When a visitor calls a page that doesn't exist, Joomla looks in your default template directory for a file called error.php. Then it dishes that up for the visitor. Let's face it most templates don't have it so Joomla goes to plan B. It looks in the /templates/system for the default error.php page and serves that up. That's the ugly one we've all grown to hate.
The code in this article works in all versions of Joomla. Yep, you read that right, it works in all of them!
The simple answer is to build a custom error.php page in your template. Go into your file manager, whatever you're using on your webhost and navigate to your default template's base directory. That would be something like /templates/my_default_template/. Create an empty file there called error.php
Before you do that, however, you'll need to create a handsome 404 page on your site. Just create an article giving whatever message you'd like. Set the metadata robots to 'noindex,nofollow'. Maybe add a link to your home page. Get the URL (you may want to create one by creating a menu item on a "hidden" menu). Then cut & paste these lines of code, remember to add your 404 page url in the code:
<?php /* This is standard for all Joomla files. It says, * "If you're not already in Joomla! go away!" */ defined('_JEXEC') or die; /* This line does three things: * * First JURI::root() asks Joomla for the root url * for your site (e.g., http://www.mysite.com) * * Second, file_get_contents() browses the web and * gets the page you're asking for (in this case your * site's JURI::root()) * * Finally, echo shows it to your visitor. */ echo file_get_contents( JURI::root() . 'the-url-of-my-404-page' );
That's it. Your server knows it can't find the page, so your web server will report a "404-page not found" error (just the way Google likes it). But your visitor will see the warm open arms of your home page, instead of the default Joomla you're an idiot slap in the face page.
In case you don't like all my comments, here's a version with the comments removed.
<?php defined('_JEXEC') or die; echo file_get_contents( JURI::root() );
That's just as easy to do. Remember, the simple version (above) used JURI::root() to get Joomla! to tell us the home page? Just tell Joomla! to change the path, and use this code instead:
<?php defined('_JEXEC') or die; // replace '/the-path-you-want' with ... well, the path you want :) echo file_get_contents( JURI::root() . '/the-path-you-want' );
Enjoy!