0

I have three RewriteRules I am trying to implement but I cannot get all 3 to work together:

RewriteRule ^products$ http://%{SERVER_NAME}/en_ca/products [L,R=301]

RewriteRule ^products/(.*)$ http://%{SERVER_NAME}/en_ca/$1 [L,R=301]

RewriteRule ^products/(.*)/(.*)$ http://%{SERVER_NAME}/en_ca/$2 [L,R=301]

What I am trying to achieve is as follows:

  1. www.domain.com/products > www.domain.com/en_ca/products
  2. www.domain.com/products/product-name > www.domain.com/en_ca/product-name
  3. www.domain.com/products/cat/product-name > www.domain.com/en_ca/product-name

I can get 1 & 2 working but 2 prevents 3 working. Is there a way to ensure the URL has 3 parts or is there a better way to do this?

Thanks

Rob Fyffe
  • 103
  • 1

1 Answers1

0
# rule 1
RewriteRule ^products/?$ /en_ca/products [L,R=301]
# rule 2
RewriteRule ^products/([^/]+)/?$ /en_ca/$1 [L,R=301]
# don't use this one since it would allow for greater than 2 /
# products/cat/cat_food/diners will write: en_ca/cat_food/diners
# RewriteRule ^products/(.*)/(.+)$ /en_ca/$2 [L,R=301]
# rule 3
# to be extra careful to only include 2 /, do it this way:
RewriteRule ^products/([^/]+)/([^/]+)/?$ /en_ca/$2 [L,R=301]

That should work. Don't use the greedy .* (anything, including /), use the limiting [^/]* (anything but a /, that is). It's also safer to change *, zero or more, to +, which is 1 or more.

I believe you may have an error elsewhere, to review what happens here:

Rule 1: take only /products, and change it to: en_ca/products. Anything following will not trigger because now the path starts with en_ca. You can get rid of the: http://%{SERVER_NAME} in the rewrite by the way. /en_ca/$1 is enough.

Rule 2: take /products/product_name and rewrite it to: /en_ca/product_name Now no further rule will change it because it no longer starts with products

Rule 3 will only trigger if the path contains 2 / and starts with products, so neither rule 1 or 2 could create a url that will be rewritten by the 3rd rule.

Adding /? (0 or 1 trailing slashes) protects you also against people who enter /products/ or products/product_name/

Lizardx
  • 210
  • 1
  • 8
  • Thanks for this, however this is still not right. Loading '/products/product-name' results in '/en_ca/products/product-name' rather than '/en_ca/product-name. – Rob Fyffe Nov 13 '15 at 03:32
  • Hmm, let me check, don't see why it would – Lizardx Nov 13 '15 at 03:35
  • No, I'm not seeing it, are you sure you're running only these rules? I tested these with a shell and they work. Did you enter the exact rules I put above? Since each test only activates when the path starts with products, the following rewrites can't impact them. The second must change: '/products/product-name to: /en_ca/product-name, the third will not act on that, but will change: /products/cat/product-name to: /en_ca/product-name. I'm going to stick with this, I believe there must be another rule somewhere. In fact your first try would create that result, but mine can't. – Lizardx Nov 13 '15 at 03:45
  • You are right, I had another conflicting rule. This now works perfectly. Thank you very much. – Rob Fyffe Nov 13 '15 at 04:09