Wednesday, November 17, 2021

Redirect http to https

http://
http://www
to non-www

To work with www you have to add one more binding that point to www
There should be 4 binding:
  • Host name websiteurl.com with http
  • Host name www.websiteurl.com with http
  • Host name websiteurl.com with https. Check require server name indication.
  • Host name www.websiteurl.com with https. Check require server name indication.
<system.webServer>
<rewrite>
    	<rules>
	        <rule name="Force HTTPS Redirection" stopProcessing="true">
	          <match url=".*" />
	          <conditions>
	            <add input="{HTTPS}" pattern="OFF"/>
	          </conditions>
	          <action type="Redirect" url="https://{HTTP_HOST}/{R:0}" redirectType="Permanent"/>
	        </rule>
	        <rule name="Remove www." stopProcessing="true">
	          <match url="(.*)" ignoreCase="true" />
	          <conditions>
	            <add input="{HTTP_HOST}" pattern="^www\.yoursite\.com$" />
	          </conditions>
	          <action type="Redirect" url="https://yoursite.com/{R:1}" redirectType="Permanent" />
	        </rule>
        </rules>
    </rewrite> 
</system.webServer>