RewriteRule Not Working in Joomla 4.x

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Locked
sazbaztaz
Joomla! Intern
Joomla! Intern
Posts: 66
Joined: Thu Mar 18, 2010 2:20 pm

RewriteRule Not Working in Joomla 4.x

Post by sazbaztaz » Fri Apr 15, 2022 10:54 am

Hi I'm trying to migrate my Joomla 3.x website to Joomla 4.x and I'm having an issue with RewriteRules in my .htaccess file.

Here's an example...

To narrow down the possible fault causes I created a simple .htaccess file with just these two lines...

Code: Select all

RewriteEngine On
RewriteRule ^test(.*)$ /index.php?option=com_mycomponent&view=listings [NC]
If I go to https://www.mywebsite.com/test I get a 404 Page Not Found error.

If i put the following in my index.php file...

Code: Select all

echo '<pre>';
print_r($_GET);
echo '</pre>';
exit();
I get...

Array
(
[option] => com_mycomponent
[view] => listings
)


So the .htaccess is working fine and variables are passed so why is Joomla displaying 404 Page?

If I access it directly like https://www.mywebsite.com/index.php?opt ... w=listings it's working fine.

This is working fine in Joomla 3.x but not in Joomla 4.x

Any thoughts?

sazbaztaz
Joomla! Intern
Joomla! Intern
Posts: 66
Joined: Thu Mar 18, 2010 2:20 pm

Re: RewriteRule Not Working in Joomla 4.x

Post by sazbaztaz » Sun Apr 17, 2022 12:51 am

Ok so I've tested this on my localhost server and it's definitely Joomla 4.x causing this problem. I'm using Xampp, php 8.1.4, and the latest Joomla! 4.1.2 Stable [ Kuamini ] 30-March-2022

I've downloaded the latest 3.x version and installed on the same server. Everyhting works fine.
In .htaccess just after RewriteEngine On I put...

RewriteRule ^login$ index.php?option=com_users&view=login [L]

In the Joomla 3.x .htaccess this works just like it's supposed to but for some reason Joomla 4.x will throw a page not found error?

sazbaztaz
Joomla! Intern
Joomla! Intern
Posts: 66
Joined: Thu Mar 18, 2010 2:20 pm

Re: RewriteRule Not Working in Joomla 4.x

Post by sazbaztaz » Sun Apr 17, 2022 2:04 am

Ok I've traced down the 404 page is coming from \libraries\src\Router\Router.php

On line 159...
if (strlen($uri->getPath()) > 0)
{
throw new RouteNotFoundException(Text::_('JERROR_PAGE_NOT_FOUND'));
}


In Joomla 3.x this looks like...

Code: Select all

if (strlen($uri->getPath()) > 0 && array_key_exists('option', $vars)
	&& ComponentHelper::getParams($vars['option'])->get('sef_advanced', 0))
{
	throw new RouteNotFoundException(\JText::_('JERROR_PAGE_NOT_FOUND'));
}
Since I don't think there will be a workaround around this at least not anytime soon I will just update to the lates 3.x and continue using it.

It works with the latest PHP 8.1 still good for years to come.

Having my custom RevriteRules is CRITICAL in my Joomla procests...

Anyway, any thaughts about this stupid change (in my optinion) I'd like to discuss...

Richard67
Joomla! Explorer
Joomla! Explorer
Posts: 270
Joined: Fri Sep 16, 2011 6:13 pm
Location: Germany
Contact:

Re: RewriteRule Not Working in Joomla 4.x

Post by Richard67 » Sun Apr 17, 2022 9:58 am

Anyway, any thaughts about this stupid change (in my optinion) I'd like to discuss...
In J4 there is no difference anymore between legacy and advanced routing. Therefore the parameter "sef_advanced" has been removed in J4 and so the condition for its check at the place you have mentioned.

So why is that change stupid in your opinion?

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 30936
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: RewriteRule Not Working in Joomla 4.x

Post by Per Yngve Berg » Sun Apr 17, 2022 11:04 am

I don't understand why you need rewrite rules in the .htaccess. That should have been taken care of by the router.php in your component.

Besides the Legacy/New Routing system only applies to com_content (Introduced in J3.8).

sazbaztaz
Joomla! Intern
Joomla! Intern
Posts: 66
Joined: Thu Mar 18, 2010 2:20 pm

Re: RewriteRule Not Working in Joomla 4.x

Post by sazbaztaz » Sun Apr 17, 2022 1:16 pm

Thanks for the reply guys.

I have been using joomla since the very earlyest version back in 2003. Actually it was Mambo back then but when the two separated I continued to use Joomla. So far I've done dozens of custom components...

As for why I don't like the new Joomla 4.x...

Now let me start that this might only be me getting used to the folder structure of the components and the menu layout in the admin etc. and then later I will say "ok this actually makes sense and it's better...".

Eg the first thing I do when I install Firefox and Chrome (I use Firefox) is enable the "Menu Bar". File, Edit, View, History, Bookmarks, Tools, Help etc and you navigate yourself from there and NOT from icons and some sort of hieroglyphs. It just makes sense like the menu in the old Joomla 3.x

So when I installed Joomal it and the first thing that randomly popped into my mind... This is like wordpress!

The folder structure in the components as well... I made more sense and it was more organised in a logigal structure.

And now, I cannot use Url Rewrite from .htaccess?

Yes, I could do a router for my component but it takes at least 200 x more effort and time than writing few lines in the .htaccess (correct me if I'm wrong please).


Locked

Return to “Joomla! 4.x Coding”