404 redirect to homepage via error.php (Joomla)


#1

This is not a DH issue, but advice would be appreciated.

I would like 404 errors to redirect to my homepage instead of a 404 error page. I came across the resource below. I am wondering if anyone with knowledge on this sees any reason it would be a bad idea to add this to my error.php file. I tested the code and it appears to work, but I do not want to create an issue inviting hackers or otherwise break my site. Thank you for your time.

[quote] There are two ways you can go. The first would and simplest would be to redirect all 404 errors back to your home page. The Second would be to redirect the 404 error to a specific article on your website.

  1. Copy the file templates/system/error.php to your default template location templates//error.php using either your hosts file manager or with a FTP client such as FileZilla.

  2. Open error.php for edit

Change this original code near the top of the file

// no direct access
defined( ‘_JEXEC’ ) or die( ‘Restricted access’ );
?>

To this (add the stuff in red). Don’t forget to change to your web site name!

// no direct access
defined( ‘_JEXEC’ ) or die( ‘Restricted access’ );
if (($this->error->code) == ‘404’) {
echo file_get_contents(‘http://www..com’);
} else {
?>

Finally at the very bottom of the file add the following

<?php } ?>
  1. Your done. Now you will not loose your site viewers to the dreaded 404 error pages. At this point they will be redirected to your home page. [/quote]

#2

The instructions being given there are describing a really, really bad way of going about that. To be specific, the changes they’re recommending will make it so that, when a user requests a page on your site that does not exist, the script will start up a second request to your site to get the contents of your home page, then paste the results into the page that had the error. Among other issues, this will end up dramatically increasing the load on your server if something is going on that is causing a lot of 404 errors. It may also cause some confusion for search engines, since all of these “404” pages will now have what appears to be real content on them.

I’m not quite familiar enough with Joomla to give exact directions for how to do this properly, but the correct way to go about this would be to make your error pages actually redirect to the home page, not just include the home page on them.


#3

Thank you so much for your time. Your reply makes perfect sense. Something told me it wasn’t a good idea, but I’m quite ignorant to this kind of thing. I will have to look into another option. Thanks again, I really appreciate your response.