Use IIS Url Rewrite module to redirect from http to https


About IIS Rewrite Module

First if you want to use it, you have to download and install to your web server. It is support IIS7, IIS7.5, IIS 8, IIS 8.5, IIS 10. List of features include, Rules-based Url rewriting engine, Wildcard pattern matching,and to check more see here.

At this blog, I'll show a case as you want to direct as following scenario.

If use type following url

  • http://www.example.com
  • http://example.com
  • https://www.example.com
  • https://example.com

Will go to https://example.com

Anyway, following is the code you need to add to web.config.

<rule name="example.com HTTP to HTTPS redirect" stopProcessing="true">
  <match url=".*" />
	<add input="{HTTPS}" pattern="off"/>
	<add input ="{HTTP_HOST}" pattern="example.com"/>
  <action type="Redirect" url="https://example.com/{R:0}" redirectType="Permanent" />
<rule name="example.com www to none-www redirect" stopProcessing="true">
  <match url="(.*)"/>
	<add input ="{HTTP_HOST}" pattern="www.example.com"/>
  <action type="Redirect" url="https://example.com/{R:0}" redirectType="Permanent"/>

Relative Blogs