skip to Main Content

Say I have a product page /GB/en/cat/product/abc/cde/*** is a good valided 200 page,
But people could accidently type /GB/en/cat/Product/abc/cde/*** or /GB/en/cat/PRODUCT/abc/cde/*** (as my content page is case sensitive) these two url causing problem for my seo purpose
So I need to have rewrite rule in dispatcher to handle anything like /GB/en/cat/Product/abc/cde/*** or /GB/en/cat/PRODUCT/abc/cde/*** ,transfer them to /GB/en/cat/product/abc/cde/***

Below is my current try out, but it only works when user types in /gb/en/cat/Product/... or /gb/en/cat/PRODUCT/... then it can successfully change to /GB/en/cat/PRODUCT/abc/cde/***

RewriteCond %{ENV:PAGE_REQUEST} ^true$
  RewriteCond %{REQUEST_URI} !^/[A-Z]{2}/[a-z]{2}
  RewriteRule ^/([a-zA-Z]{2})/([a-zA-Z]{2})(.*) "/${toupper:$1}/${tolower:$2}/${tolower:$3}" [R=301,L]

If user types in /GB/en/cat/PRODUCT/... the rewrite rule won’t work…
Any code suggestions would be really apprecaited.
Thanks

2

Answers


  1. Your second rewrite condition limits the rule:

    RewriteCond %{REQUEST_URI} !^/[A-Z]{2}/[a-z]{2}
    
    Login or Signup to reply.
  2. As Vlad said: your second condition will limit the rule, only if both conditions are met, the rule will be applied. Your second condition says to only apply the rule if the URI does not start with two upper case characters, followed by two lower case characters. So /GB/en/cat/PRODUCT/... won’t match since the second condition is not met. This means that the rule is not applied and the upper case PRODUCT is not fixed. You can just remove this second condition since your rule won’t change the correct cased characters.

    As also already said by rakhi4110, your third group in your rewriterule: (.*) will include the / after the two letters of the language, so if you end the rule with /${tolower:$3}, you will end up with two slashes in the URI, to fix that you can either remove the slash at the end, or add one in between groups 2 and 3:

    RewriteRule ^/([a-zA-Z]{2})/([a-zA-Z]{2})/(.*) "/${toupper:$1}/${tolower:$2}/${tolower:$3}" [R=301,L]
    

    or

    RewriteRule ^/([a-zA-Z]{2})/([a-zA-Z]{2})(.*) "/${toupper:$1}/${tolower:$2}${tolower:$3}" [R=301,L]
    

    (IMO the first one is cleaner code)

    So to conclude, the following should work:

    RewriteCond %{ENV:PAGE_REQUEST} ^true$
    RewriteRule ^/([a-zA-Z]{2})/([a-zA-Z]{2})/(.*) "/${toupper:$1}/${tolower:$2}/${tolower:$3}" [R=301,L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search