Serj Sagan
Serj Sagan

Reputation: 30218

Regex in Web.config Rewrite Rules for partial lowercase URL

For SEO I want my URLs to be consistent but I don't want that to affect any query parameters. So for example: These URLs:

should all become:

However this one:

should be:

I kind of have something that works sometimes with this:

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

Upvotes: 1

Views: 344

Answers (1)

Serj Sagan
Serj Sagan

Reputation: 30218

Thanks to the helpful comments by John,I got it to work like this:

    <rule name="LowerCaseRule" stopProcessing="true">
      <match url="^(.*?)(\?.*|$)" ignoreCase="false" />
      <conditions logicalGrouping="MatchAny">
        <add input="{SERVER_NAME}" pattern="[A-Z]" ignoreCase="false" />
        <add input="{PATH_INFO}" pattern="[A-Z]" ignoreCase="false" />
      </conditions>
      <action type="Redirect" url="{ToLower:{R:1}}{R:2}" redirectType="Permanent" />
    </rule>

Upvotes: 1

Related Questions