3
votes

On IIS 10 with URL Rewrite Module 2.0 I need 2 rules

1) HTTP to HTTPS

2) WWW to non-WWW

First one created by Blank rule template. For second one I use Canonical domain name template.

In my web.config rules likes like this:

<rewrite>
  <rules>
    <rule name="ForceHttps" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTPS}" pattern="^OFF$" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
    </rule>
    <rule name="CanonicalHostNameRule1" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^cooltechunder\.com$" negate="true" />
      </conditions>
      <action type="Redirect" url="https://cooltechunder.com/{R:1}" />
    </rule>
  </rules>
</rewrite>

All cases works fine expect one starting with: http://www.

See this image for results I have: https://i.imgur.com/h2l3Yw6.png

2

2 Answers

2
votes

You wrote stopProcessing="true".

This means that, if the rule matches, subsequent rules will be skipped.

From the documentation:

A rule may have the StopProcessing flag turned on. When the rule action is performed (i.e. the rule matched) and this flag is turned on, it means that no more subsequent rules will be processed and the request will be passed to the IIS request pipeline. By default, this flag is turned off.

It seems to me that this is the situation you are describing that you did not want.

So, remove it.

2
votes

Ok, My problem was different and related to bindings.

I have to specify 'Host Name' in bindings, as specific ports used by other websites also

And I forgot to add 'www' versions of bindings also.

Now my bindings looks like this: https://i.imgur.com/Lhdv4nS.jpg

Also I have changed rewrite code to more compact one:

<rewrite>
  <rules>
    <rule name="Https and non-www">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="^OFF$" />
        <add input="{HTTP_HOST}" pattern="^cooltechunder\.com$" negate="true" />
      </conditions>
      <action type="Redirect" url="https://cooltechunder.com/{R:1}" />
    </rule>
  </rules>
</rewrite>