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: https://www.iis.net/downloads/microsoft/url-rewrite
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 http://dbaland.wordpress.com, the web server will automatically redirect them to https://dbaland.wordpress.com.
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 = '.wordpress.com'
$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'}
#Action
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. .wordpress.com
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 http://dbaland.wordpress.com and observing that the after the website has loaded, the URL is now https://dbaland.wordpress.com
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:

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


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *