Home > Software engineering >  How to use .htaccess file to redirect old URL (with article id) to new URL (without article id)?
How to use .htaccess file to redirect old URL (with article id) to new URL (without article id)?

Time:08-31

I have a Joomla-based website using K2 (CCK) for content creation. Joomla is updated to J4. It is a major release of Joomla and unfortunately, the K2 team is not updating the K2 for making it J4 compatible. So I have moved all the articles to Zoo (CCK).

Now the problem is with the URLs. The structure of the old URL is as example.com/topics/category/item/12345678-this-is-the-article-alias

after migration the new URLs are example.com/topics/category/item/this-is-the-article-alias. I have Googled but have not found any method to redirect old URLs to new URLs. I just want to remove the article id (numbers/digits) with - which is just after the word item/ and before the first - coming after the word item/ in the old URL.

I have tried the rule RewriteRule ^(.*)item/(\d \-)?(.*) /$1$3 [R=301,L] but it is removing item/ as well from the URL and I want to keep it, just want to only remove article id.

CodePudding user response:

You are actually pretty close. You may use this rule to redirect. Make sure to keep this rule at the top after RewriteEngine On line:

RewriteRule ^(. /item/)\d -(. )$ /$1$2 [R=301,L,NC,NE]

You tried this rule:

RewriteRule ^(.*)item/(\d \-)?(.*) /$1$3 [R=301,L]

Which is matching item/ but not capturing in any of the capture groups hence it is left out in target.

CodePudding user response:

With your shown samples and attempts please try following .htaccess rules. Make sure to clear your browser cache before testing your URLs. This is doing external redirect for URL as per requirement.

RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(topics/category/item)/[^-]*-(\S )\s [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]
  • Related