Barto
Barto

Reputation: 377

IIS-URL Rewrite, URL rule to lowercase except querystring

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)

Upvotes: 4

Views: 6000

Answers (2)

GC.
GC.

Reputation: 1244

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.

Upvotes: 2

Lorenz Meyer
Lorenz Meyer

Reputation: 19915

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>

Upvotes: 7

Related Questions