mrmowji
mrmowji

Reputation: 944

Redirect all URLs to HTTPS/WWW versions using IIS

I have a website and just purchased an SSL. I want all URLs to redirect to their HTTPS/WWW versions. For example:

1. http://mywebsite.com       ->   https://www.mywebsite.com
2. https://mywebsite.com      ->   https://www.mywebsite.com
3. http://www.mywebsite.com   ->   https://www.mywebsite.com
4. https://www.mywebsite.com  ->   [No redirect needed]

I already tried some solutions: this and this from marked-as-answer approaches. But almost all of answers return "Not Found. HTTP Error 404" for case 2, i.e. https://mywebsite.com didn't redirect to https://www.mywebsite.com but returned 404 instead.

Already-tried solutions:

1.

<rule name="Redirect top domains with non-www to www" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^([^\.]+)\.([^\.]+)$" />
      </conditions>
      <action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
      <serverVariables>
        <set name="Redirect" value="false" />
      </serverVariables>
</rule>
<rule name="Force HTTPS" enabled="true" stopProcessing="true">
      <match url="(.*)" ignoreCase="false" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
 </rule>

2.

<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.zzz.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

It seems some other people have this problem too. But I couldn't find the right solution.

Upvotes: 1

Views: 268

Answers (1)

mrmowji
mrmowji

Reputation: 944

The problem was in the Site Bindings section of IIS. There was no record for https://mywebsite.com. I added this record and the solutions in the question worked. For more info about this feature, visit this page.

Upvotes: 1

Related Questions