How to redirect https://domain.com to https://www.domain.com using IIS?

Solution 1:

Yes, but not how you think.

  • You will need a valid certificate for domain.com
  • You will need domain.com and www.domain.com to have separate public IP addresses

If you can't do that, you're out of luck, unfortunately. You can't use HOST headers to differentiate between the two requests, because that's far too late (thus, separate IP addresses). And unless you have a valid certificate, the browser is going to choke, so you also need the 2nd cert.

There's no way around these requirements, I'm afraid.

Solution 2:

I had exact same requirement like redirect all https://domain.com to https://www.domain.com and also redirect all http to https, here how I achieved and get it working IIS 8.5. I used following code in web.config file:

<system.webServer>
  <!--Maybe some other settings-->
      <rewrite>
          <rules>
              <rule name="http to https" stopProcessing="true">
                  <match url="(.*)" />
                  <conditions>
                      <add input="{HTTPS}" pattern="^OFF$" />
                  </conditions>
                  <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
              </rule>
              <rule name="Redirect to www" patternSyntax="Wildcard" stopProcessing="true">
                 <match url="*" />
                 <conditions>
                   <add input="{HTTP_HOST}" pattern="yourdomainname.com" />
                 </conditions>
                 <action type="Redirect" url="https://www.yourdomainname.com/{R:0}" />
              </rule>
          </rules>
      </rewrite>
  <!--Maybe some other settings-->
</system.webServer>

This worked like charm. Actually I generated above code in web.config by installing "URL Rewrite Module" using Web Platform Installer. After installation I found this module in IIS 8.5 then I added new rules. Hopefully, you can directly write this code in your web.config to get things done.