dotnetnoob
dotnetnoob

Reputation: 11330

Url rewrite to lower case not working

I have the following rewrite rules in my web.config. The canonical rule works, however the lower case rule doesn't.

I'm trying to test it like this: www.mysite.com/UPPERCASE. I would have expected the url to be transformed to www.mysite.com/uppercase, but it stays in uppercase. What am I doing wrong?

<rewrite xdt:Transform="Insert">
  <rules>
    <rule name="LowerCaseRule" patternSyntax="ExactMatch">
      <match url="[A-Z]" ignoreCase="false"/>
      <action type="Redirect" url="{ToLower:{URL}}"/>
    </rule>
    <rule name="CanonicalHostName">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^www.mysite.com$" negate="true" />
      </conditions>
      <action type="Redirect" url="{MapSSL:{HTTPS}}www.mysite.com/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>
  <rewriteMaps>
    <rewriteMap name="MapSSL" defaultValue="OFF">
      <add key="ON" value="https://" />
      <add key="OFF" value="http://" />
    </rewriteMap>
  </rewriteMaps>
</rewrite>

Upvotes: 3

Views: 4189

Answers (3)

kamranicus
kamranicus

Reputation: 4387

This was better for me. I noticed {URL} does not properly resolve when you have a path like cassete.axd/scripts/myscript.js?xxx, it will redirect to cassette.axd?xxx instead.

    <rule name="LowerCaseRule - HTTPS">
      <match url="[A-Z]" ignoreCase="false"/>
      <conditions>
        <add input="{HTTPS}" pattern="on" ignoreCase="true"/>
      </conditions>
      <action type="Redirect" url="https://{ToLower:{HTTP_HOST}}{ToLower:{PATH_INFO}}" appendQueryString="true"/>
    </rule>

    <rule name="LowerCaseRule - HTTP">
      <match url="[A-Z]" ignoreCase="false"/>
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true"/>
      </conditions>
      <action type="Redirect" url="http://{ToLower:{HTTP_HOST}}{ToLower:{PATH_INFO}}" appendQueryString="true"/>
    </rule>

Hope this helps someone.

Upvotes: 0

RajeshK
RajeshK

Reputation: 21

Try this

<rule name="LowerCaseRule" stopProcessing="true">
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{URL}" pattern=".*[A-Z].*" ignoreCase="false" />
        </conditions>
        <action type="Redirect" url="{ToLower:{URL}}" />
    </rule>

Upvotes: 0

cheesemacfly
cheesemacfly

Reputation: 11762

You should remove patternSyntax="ExactMatch" from the rule LowerCaseRule because in your case, you want to use the regular expression system (which comes by default or by setting patternSyntax="ECMAScript").

So your rule should be:

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

Upvotes: 4

Related Questions