Weisen
Weisen

Reputation: 612

htaccess strip part of string in url

I have a rewrite rule that redirects a specific path like

http://mev-hongkong.com/product/boardwalk-long-shorts/ to https://mothersenvogue.com.hk/product/boardwalk-long-shorts/

So my rule looks like this

RewriteCond %{HTTP_HOST} ^(www\.)?mev-hongkong\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^product/boardwalk-long-shorts(/.*)?$ https://mothersenvogue.com.hk/product/boardwalk-long-shorts$1 [L,R=301,NC,NE,QSD]

I also have a URL of the same path but the difference is that it has a "-2" on the end like

http://mev-hongkong.com/product/boardwalk-long-shorts-2/

What rule should I include on the original URL for me to be able to catch the "-2" on my old domain?

I tried making my RewriteRule like this (adding .* before the slash) but it didn't work.

 RewriteRule ^product/boardwalk-long-shorts(.*/.*)?$ https://mothersenvogue.com.hk/product/boardwalk-long-shorts$1 [L,R=301,NC,NE,QSD]

Thanks!

Upvotes: 1

Views: 27

Answers (1)

anubhava
anubhava

Reputation: 784878

You may use this rule:

RewriteCond %{HTTP_HOST} ^(www\.)?mev-hongkong\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^product/boardwalk-\w+-shorts(?:-[^/]+)?(/.*)?$ https://mothersenvogue.com.hk/product/boardwalk-long-shorts$1 [L,R=301,NC,NE,QSD]

(?:-[^/]+)? makes -2/ an optional match

Upvotes: 1

Related Questions