0
votes

I have a problem that I can't seem to find any solution to online. All searches are for http to https or non-www to www but without the http in front.

My site will not redirect https://domain.com to https://www.domain.com. It redirects domain.com to www.domain.com just fine though, it's only when the https is already entered does it not redirect and thus gives an error.

I have this for redirecting http to https:

<rule name="HTTP to HTTPS redirect" stopProcessing="true">
    <match url="(.*)" />
        <conditions>
              <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        </conditions>
    <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>

Edit: after experimenting, it manages to redirect http://domain.com to https://www.domain.com correctly (i.e from without s to with s and www).

Yet still doesn't from https://domain.com to https://www.domain.com

1

1 Answers

0
votes

Figured it out, after adding this:

<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAny">
      <add input="{HTTP_HOST}" pattern="^[^www]" />
      <add input="{HTTPS}" pattern="off" />
  </conditions>
  <action type="Redirect" url="https://www.domain.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

And most importantly, removing the hostname field 'www.domain.com in IIS > Site Bindings > Edit > Host Name, and unticking 'Require Server Name Indication'. Having my domain entered here was causing the above code to not work. I originally added this thinking it was necessary, but not so.