Support

Admin Tools

#34600 redirect 404 using .htaccess

Posted in ‘Admin Tools for Joomla! 4 & 5’
This is a public ticket

Everybody will be able to see its contents. Do not include usernames, passwords or any other sensitive information.

Environment Information

Joomla! version
n/a
PHP version
n/a
Admin Tools version
n/a

Latest post by nicholas on Tuesday, 23 February 2021 02:26 CST

baroen

I want to redirect all 404 error traffic to my homepage.
I guess I can do that best by using .htaccess.
What line of code should I enter in the HTACCESS maker in what field to accomplish this?

nicholas
Akeeba Staff
Manager

You can't, really. 404s need to be handled by Joomla's index.php. That's how SEF URLs work.

When you enter the URL https://www.akeeba.com/support/admin-tools/Ticket/34600:redirect-404-using-htaccess.html in your browser it contacts our server and asks it for a file named "34600:redirect-404-using-htaccess.html" in the folder "support/admin-tools/Ticket". Of course no such folder or file is physically present. This creates a 404. This 404 is picked up by the Joomla core SEF block towards the bottom of the the .htaccess file which tells the web server to load the index.php file instead. The index.php file is Joomla's entry point. Joomla sees the URL path ("support/admin-tools/Ticket/34600:redirect-404-using-htaccess.html") and runs it through its SEF parser. Eventually it comes up with something like option=com_ats&view=Ticket&id=34600&format=html&Itemid=123 which is now handled internally as a non-SEF URL, displaying the page of the ticket we are discussing on.

If Joomla's SEF parser can't come up with a viable page it raises a RuntimeException with the code 404. This triggers Joomla's internal error handler which makes it load the template's error.php page. If this is missing it loads templates/system/error.php to show a basic error page. In Joomla 4 there's a further fallback to Symfony's error handler.

So, the correct way to handle 404 redirection is adding custom code to your template's error.php page, but that's beyond the scope of Admin Tools support.

For an example of custom handling of 404 errors see https://www.akeeba.com/this-page-does-not-exist.html. Our template's error.php retrieves the exception Joomla is handling and shows its code and message in the gray bar. The error code is further used to decide the caption above the gray bar, in the teal area. 404s print out "🗺 I should have taken that left turn in Albuquerque" (yup, that's a reference to old Bugs Bunny cartoons).

You could use the same trick to do 404 redirection to the home page.

However, I would NOT recommend this at all. Browsers and search engines see these redirections and understand them as either temporary redirections or permanently moved pages depending on how you implement them. It does not communicate that the link is broken. Search engines will keep on pounding on those non-existent pages and will complain about duplicate content in the second case. They will not delist them. This will have a negative effect for your site.

It's best to implement a custom error page which links to your home page and includes a search box. That's what we did. The error page sets the HTTP response code to 404, notifying search engines that the page is no longer there (avoiding any indexing problems), and the user sees a link to the home page and a way to find what they are looking for. It's a far better experience for you, the search engines and the user.

Nicholas K. Dionysopoulos

Lead Developer and Director

🇬🇷Greek: native 🇬🇧English: excellent 🇫🇷French: basic • 🕐 My time zone is Europe / Athens
Please keep in mind my timezone and cultural differences when reading my replies. Thank you!

Support Information

Working hours: We are open Monday to Friday, 9am to 7pm Cyprus timezone (EET / EEST). Support is provided by the same developers writing the software, all of which live in Europe. You can still file tickets outside of our working hours, but we cannot respond to them until we're back at the office.

Support policy: We would like to kindly inform you that when using our support you have already agreed to the Support Policy which is part of our Terms of Service. Thank you for your understanding and for helping us help you!