404 page not found handling

Once we had decided against using sh404sef, we were still left with the need to handle '404 Page Not Found' errors. Clear and explicit information on this wasn't particularly easy to find, so I thought I'd document how we did it here.

 In J!15, errors are normally handled by - surprise! - a template file. Look in the the system template folder for the file error.php. Standard J!15 error handling sends the user to this page for any unrecoverable error. Including 404 errors. If you copy this file to the root folder of your own template, you can then modify it to your needs.

In very simplified form, here is what we did:

if ( $this->error->code == 404)
{
header('Location: ' . JRoute::_('index.php?Itemid=XX'));
exit();
}

Create a menu item someplace on your site to send the user on a 404 error and replace that XX above with the menu's id number.

From these humble beginnings there are a number of things you can do to fancy it up. For instance, I like giving my designers and users options. So rather than hardcode the menu id into the code as described here, I coded it as a template parameter.

For our latest launch, the client had a number of old URLs that he was very concerned about losing. So another enhancement I made to the above code was to detect what URL the user was attempting to access. If it was in the list of critical URLs, I did a 301 redirect to the appropriate URL on the new site.