Change URLs to lowercase

Topics: Developer Forum, User Forum
Nov 8, 2009 at 11:26 PM
Edited Nov 8, 2009 at 11:27 PM

Hi all,

I have tried several combinations of rewrite filters and looked through various discussions as well as the documentations on #L etc. But i just can't seem to successfully write a rule to change my URLs to lowercase.

Can someone please post exactly what rules you would need to convert ALL urls in a domain to lowercase.

I'm using version 1.26 and i needs the URLs to visibly change to lowercase. I also need 301 redirects from the mixedcase URL to the new lowercase URL.

I'd really appreciate some help on this please.

 

 

Thanks in advance

Coordinator
Nov 9, 2009 at 12:29 AM
Edited Nov 9, 2009 at 12:39 AM

what have you tried?  This rule says "convert to lower case" . 

RedirectRule   ^/(.*)$     http://server-name/#L$1#E  [I,R=301]

if there is something more, show me some examples as to what you expect, in and out. eg  :" I want to put in http://foo/bar/Baz and get out... ???"

Nov 9, 2009 at 12:36 AM

Based on this example here: http://iirf.codeplex.com/wikipage?title=Case%20Folding&referringTitle=Examples

I have tried the following:

RewriteRule ^(.+)$  #L$1#E  [L]

RewriteRule .* #L$1#E [L] 
Coordinator
Nov 9, 2009 at 12:40 AM
Edited Nov 9, 2009 at 12:46 AM

OK, those are rewrites, not redirect.  You specifically asked to change the URL in the address bar, and that you wanted HTTP status of 301.  So pretty clearly you need RedirectRule, not RewriteRule. Then add in the 301 return code and I think you want something like this: 

RedirectRule   ^/(.*)$     http://server-name/#L$1#E  [R=301]

Some explanations on why your rules did not work:
Your first rule won't work because it's a RewriteRule, not RedirectRule. The second one will not work for that reason, and also, you have no "capturing group". The $1 in the replacement string references a the first "capturing group" which is denoted in the pattern with parens. Since you have no parens, there is no "first capturing group" so the second rule would always return nothing for $1, probably not what you want.

ps: You can "run" your rules against incoming URLs with the TestDriver.exe tool.  I encourage you to try it.  Running this tool on your rules would have told you what was happening:  that you were getting a rewrite and not a redirect.  It also would have clearly told you that the second rule wasn't doing anything - it would just eat your incoming URL and rewrite any incoming request to (nothing) - an empty URL.

 

Nov 9, 2009 at 12:46 AM
Edited Nov 9, 2009 at 2:42 AM

I have tried several combinations of the above (my post not yours)

I have a url in the form  -   www.mydomain.com/Category-Name.aspx   which needs to convert to www.mydomain.com/category-name.aspx

 WWW.mydomain.COM/Product-Name-For-SEO.aspx?Pid=23456&BLAH=something  which needs to convert to www.mydomain.com/product-name-for-seo.aspx?pid=23456&blah=something

 

This is what my IsapiRewrite4.ini looks like at the moment:

 

 

RewriteLogLevel 1
RewriteLog C:\temp\iirf-log.out
RewriteCond %{HTTP_HOST} ^thedeal\.co.nz [I]
RedirectRule ^/(.*) http://www.thedeal.co.nz/$1 [I,RP,R=301]
RewriteRule ^/rewritetest/(.*) /

 

RewriteLogLevel 1

RewriteLog C:\temp\iirf-log.out

RewriteCond %{HTTP_HOST} ^mydomain\.com [I]

RedirectRule ^/(.*) http://www. mydomain.com/$1 [I,RP,R=301]

RewriteRule ^/rewritetest/(.*) /

 

 

These rules work perfectly fine. 

 

Based on the above I have tried the following as well without any success:

 

 

RewriteCond %{HTTP_HOST} ^[^A-Z]*[A-Z]+.* [I]

RewriteRule ^(.*)  #L$1#E 

 

 

 

Oh and thanks for the super quick response Mr. Shade

 

 

Coordinator
Nov 9, 2009 at 12:59 AM
Edited Nov 9, 2009 at 1:03 AM

You need to do the case conversaion in the same operation as the redirect - in the same directive.

This set of rules says to redirect every URL that comes in to mydomain.com. Redirect it to www.mydomain.com and convert case when redirecting.

RewriteLogLevel 4
RewriteLog C:\temp\iirf-log.out
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RedirectRule ^/(.*[A-Z].*)$ http://www.mydomain.com/#L$1#E [R=301]

There's one thing I want you to notice in the above - the redirect happens IF::

  • the incoming domain is the simple "mydomain.com" AND
  • the URL request has at least one uppercase character

The (.*[A-Z].*) group says "any sequence that starts with zero or more characters, has at least one uppercase character, an then followed by zero or more of any character." In other words, a string with at least one uppercase character.

The redirect happens only if BOTH conditions are true. This may or may not be what you want. It may be that you want the redirect to happen if "mydomain.com" is the incoming host, AND you want the redirect to happen if the incoming request has an uppercase character. In that case i think you want 2 rules:

RewriteLogLevel 4
RewriteLog C:\temp\iirf-log.out
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RedirectRule ^/(.*)$ http://www.mydomain.com/#L$1#E [R=301]
RedirectRule ^/(.*[A-Z].*)$ http://www.mydomain.com/#L$1#E [R=301]

In this case there are 2 rules - both are redirects. The first one redirects and converts case if the hostname is mydomain.com. The second redirects if there are uppercase characters. One or the other rule will catch all your cases.

But I don't know if that's what you want. Just guessing.

Also, pay attention because I bumped the loglevel to 4 and you don't want to run that in production.