Reputation: 3589
I need to rewrite a specific word to lowercase if it has an uppercase letter in it but not if it is entirely lowercase (if it matched that it would cause an infinite loop).
I came up with this but am confused as to why it is not working:
# /MaTcHeD to matched
RewriteCond %{REQUEST_URI} ^(?i)matched(\/.*)?$
RewriteRule ^[a-z]*[A-Z]+[^\/]*(\/.*)?$ /matched$1 [R=301,L]
To me this says if url is "matched" or "Matched" or "mAtched" or "MAtched" etc. then match if there is any lowercase characters ([a-z]) at the beginning of the url followed by any one or more uppercase characters ([A-Z]) followed by any non "/" character then the rest of the url (an optional "/" followed by anything else). If those conditions are true then redirect to the lowercase "/matched".
I have browsed around and seen many solutions that match ANY word, but that would cause problems since other things are in uppercase. I just need "matched" in any case but all lowercase to redirect to the all lowercase "matched".
Upvotes: 2
Views: 2101
Reputation: 143906
You can do a couple of checks to cover this, first check that it isn't lowercase at all:
RewriteCond %{REQUEST_URI} !^(.*)/matched(.*)$
Then check if the same would match if case was ignored by using the [NC]
flag:
RewriteRule ^(.*)/matched(.*)$ /$1/matched$2 [R=301,L,NC]
At this point, you know that without case sensitivity, matched
matched, but it's not all lowercase, so you can redirect to all lowercase.
I am using the match currently in the htaccess level so I do not need the initial "(.*)/". Using that would not work. "matched" is the first path part after the domain
Try this then:
RewriteCond %{REQUEST_URI} !^/matched(.*)$
RewriteRule ^matched(.*)$ /matched$1 [R=301,L,NC]
The problem is that the %{REQUEST_URI}
variable does have a leading slash, but the URI used to match against the RewriteRule
doesn't have one (go figure).
Upvotes: 1