URL redirect with htaccess Topic is solved

Need help upgrading your Joomla! website or converting to Joomla! 4.x?

Moderator: 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.
Post Reply
User avatar
stevent
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 120
Joined: Thu Sep 15, 2005 6:05 pm
Location: Nijmegen, Netherlands
Contact:

URL redirect with htaccess

Post by stevent » Wed Jun 29, 2022 10:52 am

I've migrated my site van Joomla to Joomla 4. I've also moved to articles from K2 to Joomla articles. Over the ast few years there where thousands of articles written, so the redirects component isn't a simple solution for redirecting the old K2 url's to the new url's. I want to fix this with a line of code in htaccess, but my regular expressions skill are apparently not sufficient to get it right.

The old ur's are

Code: Select all

https://mysite.com/menuname/submenuname/item/12345-item-alias
The new url's don't have the /item and I removed the item-id (which differs since the migration) including the trailing dash. The new urls are thus:

Code: Select all

https://mysite.com/menuname/submenuname/item-alias
I thought this line of code would do:

Code: Select all

RewriteRule ^/item/([0-9]+)-(.*)?  $1 [R=301,L]
But the result is that the id is still there and that the /menuname/submenuname is also removed:

Code: Select all

https://mysite.com/12345-item-alias

gws
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4749
Joined: Tue Aug 23, 2005 1:56 pm
Location: South coast, UK
Contact:

Re: URL redirect with htaccess

Post by gws » Wed Jun 29, 2022 11:12 am

An interesting approach to url rediects from Pe7er. viewtopic.php?f=808&t=994669
Just noticed you and Pe7er are townies.

User avatar
stevent
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 120
Joined: Thu Sep 15, 2005 6:05 pm
Location: Nijmegen, Netherlands
Contact:

Re: URL redirect with htaccess

Post by stevent » Wed Jun 29, 2022 11:38 am

Yes I know Peter personally. ;-)

His solution is to mass import old and new urls Ito the redirect system. That is some effort. (I have 4000+ articles) It should be really easy, in my case, to do this in htaccess, since the structure of all the urls are identical.

User avatar
stevent
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 120
Joined: Thu Sep 15, 2005 6:05 pm
Location: Nijmegen, Netherlands
Contact:

Re: URL redirect with htaccess

Post by stevent » Thu Jun 30, 2022 7:21 am

I've figured it out. (It took me some time, since it seems the browser was changing old redirects.)

The code should be:

Code: Select all

RedirectMatch 301 ^(.*)/item/([0-9]+)[-](.*)$ $1/$3
To explain it for anyone who finds this post in de future:

$1 is for the path (menuname/submenuname) which is matched by the first (.*) and $3 is the alias which is matched by the second (.*).
Where did $2 go? That's the part with the numbers you leave out: ([0-9]+)
And the hyphen is filtered out with [-]


Post Reply

Return to “Migrating and Upgrading to Joomla! 4.x”