The Joomla part
To have control over your 404 error messages in Joomla do the following:
Create a new menu called “Hidden Menu” if it doesn’t already exist (you can ignore the module, J1.6+ just leave it un-created). Next create an Article called “404 Not found” and take note of the id number. Then create a menu item in the “Hidden Menu” pointing to the Article.
Copy the file at joomlaRoot/templates/system/error.php into your current site template.
Now edit that file, remove everything there and copy the following into it, change the aritcle id number, then save:
The .htaccess part
The above will cover the Joomla page errors like mydomain.com/notARealpage.html, but now you will want to trap errors like mydomain.com/products.htmlp5woops.
Add the following into your .htaccess file as it looks, change the article id number, save and try it out.