asp.net-core-2.2url-rewrite-moduleiis-10

IIS Redirect HTTP to HTTPS and WWW to non-WWW


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.sstatic.net/2CFX3.png


Solution

  • 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:

    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>