Forum Moderators: phranque
I have used mod rewrite quite a lot but this one is baffling me.
I want to rewrite
example.com/widget/United_Kingdom/
to
example.com/widget/?country=United_Kingdom
the lines I am using are:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^widget/([A-Za-z0-9_]+)/$ /widget/?country=$1 [R,QSA,L]
but it rewrites to
example.com/widget/?country=united_kingdom (lower case)
Does anyone see what I am doing wrong here?
Thank you
However, if the request comes through a caching proxy (e.g. corporate or ISP proxy), you might have a problem with the end-anchored RewriteCond pattern, because it won't accept a hostname with an appended port number. It's also unnecessary to explicitly test for [a-zA-Z], since you can use the [NC] flag or "\w" regex token to eliminate the need to do so. And unless you also append query strings to your 'static SE-friendly' URLs, use of [QSA] is not needed. Your regex for the requested URL also will *require* the trailing slash -- potentially a problem with type-ins or SE referrals.
Finally, using an [R] redirect flag 'exposes' your query-based script URL to the client (browser or robot) and is both unnecessary and probably very undesirable. Showing a couple of more-efficient coding options:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^widget/([a-z0-9_]+)/?$ /widget/?country=$1 [NC,L]
RewriteCond %{HTTP_HOST} ^example\.com(:[0-9]{1,5})?$ [NC]
RewriteRule ^widget/([\w]+)/?$ /widget/?country=$1 [L]
I am developing this on my local machine so no proxies etc. Ctrl f5 several times doesn't change anything. Neither does using a new example I haven't tried before.
Your first code works but I still get all lowercase in the query string
Your second code fails.
I will look at them more closely later on.
Thanks.