Knight Talks Tech

Create a http to https URL redirect in IIS with Powershell

If you are hosting a website on IIS and would like your visitors to connect securely via https, whether they specify that in their browser or not, then there are a few steps you need to take.
First of, you need to install your SSL certificate into IIS.
Then install the URL Rewrite IIS module/extension which can be obtained from Microsoft here:
To ensure that a secure connection is used we can create a http to https redirect rule. This will mean that when someone types in the URL, the web server will automatically redirect them to
This rule can be manually created but to help save time and ensure consistency, the following Powershell can be used.
$webname= 'dbaland'
$rulename = $webname + ' http to https'
$domain = ''
$inbound = '(.*)'
$outbound = 'https://{HTTP_HOST}{REQUEST_URI}'
$site = 'IIS:\Sites\' + $webname + $domain
$root = 'system.webServer/rewrite/rules'
$filter = "{0}/rule[@name='{1}']" -f $root, $rulename

#Match URL
#stopProcessing not applicable for redirects although with rewrite, it will stop further rules from running
Add-WebConfigurationProperty -PSPath $site -filter $root -name '.' -value @{name=$rulename; patterSyntax='Regular Expressions'; stopProcessing='True'}
Set-WebConfigurationProperty -PSPath $site -filter "$filter/match" -name 'url' -value $inbound
#Conditions -> Logical Grouping
Set-WebConfigurationProperty -PSPath $site -filter "$filter/conditions" -name '.' -value @{input='{HTTPS}'; matchType='0'; pattern='^OFF$'; ignoreCase='True'; negate='False'}
Set-WebConfigurationProperty -PSPath $site -filter "$filter/action" -name 'type' -value 'Redirect'
Set-WebConfigurationProperty -PSPath $site -filter "$filter/action" -name 'url' -value $outbound

In the above code, specify $webname – the first part of the URL e.g. dbaland, and then $domain – the second part of the URL e.g.
When this is executed on the web server, the rule is automatically created and can be seen by double-clicking the URL Rewrite icon in IIS.

It is also necessary to ensure that “Require SSL” is not enabled, make sure this is not set by double-clicking the SSL icon.

This can be tested by browsing to and observing that the after the website has loaded, the URL is now
The newly created rule is written into the websites web.config file. This can be seen by browsing to the website folder on the web server and editing the web.config, the following should be visible:

<handlers accessPolicy=”Read, Execute, Script” />
<rule name=”ebsIntel-tribalcollege http to https” stopProcessing=”true”>
<match url=”(.*)” />
<add input=”{HTTPS}” pattern=”^OFF$” />
<action type=”Redirect” url=”https://{HTTP_HOST}{REQUEST_URI}” />

Exit mobile version