Barton
Barton

Reputation: 143

.HTAccess redirecting rewriting pages based on partial URL pattern?

I have decided to geo-tag our e-commerce website and create sub-folders for the separate countries, i.e. UK, EU, US and CA. Currently all pages reside in:

http://www.example.com/buy/

Every product has a single page for each country. All in the buy/ sub-folder have the format of:

individual-product-name-gbp.html
individual-product-name-eur.html
individual-product-name-usd.html
individual-product-name-cad.html

http://www.example.com/buy/individual-product-name-gbp.html
http://www.example.com/buy/individual-product-name-eur.html
http://www.example.com/buy/individual-product-name-usd.html
http://www.example.com/buy/individual-product-name-cad.html

What I would like to do is 301 redirect rewrite the GBP, EUR, USD and CAD pages to the following sub-folders and format:

http://www.example.com/uk/individual-product-name.html
http://www.example.com/eu/individual-product-name.html
http://www.example.com/us/individual-product-name.html
http://www.example.com/ca/individual-product-name.html

Thank you for any help or advice.

Best wishes.

Upvotes: 0

Views: 371

Answers (1)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51721

Assuming you have several such products and pages, use this

RewriteEngine on

RewriteRule ^buy/(.*)-gbp\.html$ /uk/$1.html [R=301,NC,L]
RewriteRule ^buy/(.*)-eur\.html$ /eu/$1.html [R=301,NC,L]
RewriteRule ^buy/(.*)-usd\.html$ /us/$1.html [R=301,NC,L]
RewriteRule ^buy/(.*)-cad\.html$ /ca/$1.html [R=301,NC,L]

Upvotes: 1

Related Questions