Hardik
Hardik

Reputation: 1283

Remove words from URL with htaccess

I am trying to remove category name from url. I am using following rules for that.

To remove single category name from url i am trying following. This is working properly.

RewriteEngine On
RewriteRule ^(.*)/man/(.*).html$ /$1/$2.html [NC,R=301,L]

http://www.test.com/en/man/product-1.html
Output: http://www.test.com/en/product-1.html

To remove multiple sub categories name from url i am trying following.

RewriteEngine On
RewriteRule ^(.*)/man/(.*).html$ /$1/$2.html [NC,R=301,L]
RewriteRule ^(.*)/man/jeans/(.*).html$ /$1/$2.html [NC,R=301,L]

http://www.test.com/en/man/jeans/product-1.html
Output: http://www.test.com/en/jeans/product-1.html
**expected: http://www.test.com/en/product-1.html**

I have around 30 categories and i want to remove all category name from url using htaccess. Is there any short way? Or can we add all category name in array to check if exist then rule will remove it?

Upvotes: 2

Views: 125

Answers (1)

anubhava
anubhava

Reputation: 784898

For the issue that you are getting you can tweak your regex to use [^/]+ instead of .* because .* matches longest possible string hence first rule executes when you want your 2nd rule to execute for URI /en/man/jeans/product-1.html. Have it like this:

RewriteEngine On

RewriteRule ^([^/]+)/hands/jeans/([^/.]+)\.html$ /$1/$2.html [NC,NE,R=301,L]

RewriteRule ^([^/]+)/hands/([^/.]+)\.html$ /$1/$2.html [NC,NE,R=301,L]

You can also make first rule as generic to cover all 30 categories by using:

RewriteRule ^([^/]+)/hands/[^/]+/([^/.]+)\.html$ /$1/$2.html [NC,NE,R=301,L]

Upvotes: 1

Related Questions