West55
West55

Reputation: 496

Dynamic htaccess rewrite

I am trying to figure out how to setup some redirects in my htaccess file.

We load our stores with dynamic urls for example:

/locations/new-york

new-york being the dynamic part

We have old urls that contain different path such as

/locations/new-york/local-store

I want to be able to take all locations/dynamic-city and forward them to the new URL

So

/locations/new-york/local-store/offers

should goto /locations/new-york/current-offers

Upvotes: 0

Views: 131

Answers (1)

slapyo
slapyo

Reputation: 2991

RewriteRule ^locations/(.*)/local-store/offers locations/$1/current-offers [L,R=301,QSA]
RewriteRule ^locations/(.*)/local-store/hot-tubs locations/$1/hot-tubs [L,R=301,QSA]
RewriteRule ^locations/(.*)/local-store/pre-owned locations/$1/pre-owned [L,R=301,QSA]
RewriteRule ^locations/(.*)/local-store/client-reviews locations/$1/reviews [L,R=301,QSA]

Ok, so I'm not seeing a way to easily use 1 rewriterule to cover all 4 cases. But you can see make sure the requested url following a pattern like this.

^locations/{location}/local-store/offers

Then you take the location and place it into the new url you want to write it to.

L     # means it's the last rule, don't try to match anymore
R=301 # means redirect the old url to the new one
QSA   # means append any query string parameters to the new url

Upvotes: 1

Related Questions