4

Hi I am using the URL Rewrite module in IIS. I would create a rule to convert lowercase urls. But, I'm having problems. My goal is to do the following:

http://www.doMain.com/App/ActIon?X=1&y=3&JJ=3... => http://www.domain.com/app/action?X=1&y=3&JJ=3...

My first failed attempt was:

<rule name="LowerCaseRule1" stopProcessing="true">
  <match url="[A-Z]" ignoreCase="false" />
  <action type="Redirect" url="{ToLower:{URL}}" />
</rule>

Result:

http://www.doMain.com/App/ActIon?X=1&y=3&JJ=3... => http://www.domain.com/App/ActIon?X=1&y=3&JJ=3...

it only applies to the domain (Because the URL variable has only the domain)

My second failed attempt was:

<rule name="Convert to lower case" stopProcessing="true">  
  <match url=".*[A-Z].*" ignoreCase="false" />  
  <action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />  
</rule>

Result:

http://www.doMain.com/App/ActIon?X=1&y=3&JJ=3... => http://www.domain.com/app/action?x=1&y=3&jj=3...

This also applies to query string. Therefore I did not serve me.

My third failed attempt was:

<rule name="Convert to lower case" stopProcessing="true">  
  <match url="^(.*[A-Z].*)(\/.*)" ignoreCase="false" />  
  <action type="Redirect" url="{ToLower:{R:1}}{R:2}" redirectType="Permanent" />  
</rule>

Result:

http://www.doMain.com/App/ActIon?X=1&y=3&JJ=3... => http://www.domain.com/app/ActIon?X=1&y=3&JJ=3...

The problem this does not apply to the last path.

This causes the following rule that I need is not met:

http://www.doMain.com/App => http://www.domain.com/app

Questions:

Is there any rule that allows me to do what I need?

Is it correct to do with the module?

Because I have another alternative is to use ASP.NET routing engine (For example https://github.com/schourode/canonicalize)

Barto
  • 337
  • 2
  • 11
  • I think this is overkill to do with a URL rewrite. Just use the `routes.LowercaseUrls` config option and call it day. That will at least ensure that all URLs you generate are lowercase. Even the Canonicalize project seems like overkill if you're only using it for lowercasing. – Chris Pratt May 07 '15 at 19:02
  • 2
    Thanks for the comment, but that alone does is register all routes (Using lowercase). What I need is automatically redirected when users make a GET to a URL that contains uppercase on your path. – Barto May 07 '15 at 19:12

2 Answers2

7

There is just a little modification needed: Break the URL on the ? to separate the filename part from the query string.

<rule name="Convert to lower case" stopProcessing="true">  
  <match url="^([^?]*[A-Z][^?]*)(\?.*)?" ignoreCase="false" />  
  <action type="Redirect" url="{ToLower:{R:1}}{R:2}" redirectType="Permanent" />  
</rule>

If you use hashes (#), you probably should break on them too, because you don't want to force them to lowercase neither.

<rule name="Convert to lower case" stopProcessing="true">  
  <match url="^([^?#]*[A-Z][^?#]*)(\?.*)?(#.*)?" ignoreCase="false" />  
  <action type="Redirect" url="{ToLower:{R:1}}{R:2}{R:3}" redirectType="Permanent" />  
</rule>
Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
  • this works great, but my site has starts with subdirectory www.mysite.com/English/home.aspx www.mysite.com/French/home.aspx When applying the above rule, the English and French is not converting to lower case – carrieat Apr 27 '17 at 15:31
2

The complex regex is not necessary

<rule name="Convert to lower case" stopProcessing="true">
  <match url=".*[A-Z].*" ignoreCase="false" />
  <action type="Redirect" 
          url="http://{ToLower:{HTTP_HOST}{PATH_INFO}}" 
          redirectType="Permanent" />
</rule>

Querystring is not included in the match, and is appended to the redirect URL by default unchanged.

GC.
  • 1,214
  • 1
  • 10
  • 26