Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite changing case of string match

how to keep upper and lower case?

         

proper_bo

3:07 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



Hi all,

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

jdMorgan

4:08 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's nothing in your code that would cause this, so the most likely problem is that you're not flushing your browser cache after changing the code on the server.

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]

-or-

RewriteCond %{HTTP_HOST} ^example\.com(:[0-9]{1,5})?$ [NC]
RewriteRule ^widget/([\w]+)/?$ /widget/?country=$1 [L]

Jim

proper_bo

4:30 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



Hi,

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.

jdMorgan

4:38 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're on a Windows machine, this may be down to differences in the regular-expressions libraries, and in case-handling differences between Windows (case-insensitive) and *nix (case-sensitive).

Use Internet Options->Delete Temporary Internet Files to flush your cache if using IE.

Jim

proper_bo

9:06 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



Cheers for your help Jim,

I have excellent news.
I uploaded the file and tried it online on a NON windows server. It works perfectly.

So in summary, windows sucks.