Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 994
Hovhannes Bantikyan Avatar asked Sep 04 '25 03:09

Hovhannes Bantikyan


2 Answers

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>
like image 111
Hovhannes Bantikyan Avatar answered Sep 06 '25 21:09

Hovhannes Bantikyan


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.

like image 43
Lightness Races in Orbit Avatar answered Sep 06 '25 21:09

Lightness Races in Orbit