Home > DevOps > Redirect HTTP to HTTPS in IIS using URL Rewrite

Redirect HTTP to HTTPS in IIS using URL Rewrite

To redirect all connections which hit a website hosted in IIS on Windows Server, you need to add a rewrite rule to the web.config file. Usually you want to do this site wide so the following lines of code will go into the web.config file in the root folder of the website.

Before you go copy and pasting the content below, you need to have the URL Rewrite Module installed. This is an extension for IIS. If your website is hosted in the Microsoft Azure Cloud Platform Services, you don’t need to worry about this. But if your website is hosted within a virtual machine then you will need it.

<rewrite>
	<rules>
		<rule name="HTTPS Redirect" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
			<match url="*" negate="false" />
			<conditions logicalGrouping="MatchAny">
				<add input="{HTTPS}" pattern="off" />
			</conditions>
			<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
		</rule>
	</rules>
</rewrite>

More Documentation for creating rewrite rules for the URL Rewrite Module can be found at: https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

If you want to get additional information, you can reference some of the following links:

https://blogs.technet.microsoft.com/dawiese/2016/06/07/redirect-from-http-to-https-using-the-iis-url-rewrite-module/