Redirect HTTP to HTTPS

Programming, error messages and sample code > sample code
Redirecting all traffic from HTTP to HTTPS in IIS7 will make sure your users always access the site securely.

What you need to do to set the redirection from http to https is add the following url rewrite rule to your web.config file, then save it.

Please put the web.config file in the root folder of your website.
 
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
    <rules>
        <rule name="HTTP to HTTPS redirect" stopProcessing="true">
            <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                </conditions>
            <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
    </rules>
</rewrite>
</system.webServer>
</configuration>
 
And now test the site by going to http://www.yoursite.com and making sure it redirects.