6 Comments

For this small article, I assume that you have an Asp.Net web application running on IIS and that you have setup your SSL certificate. So navigating to https://www.yoursite.com works. This blog post will explain how to redirect all http traffic to https in several easy steps.

1. Get the Web Platform Installer (it’s free!) from Microsoft  http://www.microsoft.com/downloads/en/details.aspx?FamilyID=32b0dfe5-f139-4e1c-b412-3da39f50bbf9 

2. After you have opened the WebPI (Web Platform Installer) search for: Rewrite

image

3. Install the component! [more]

4. Open your web.config (I used notepad++ )

5. Merge this into your web.config:

<configuration>    
    <system.webServer>
        <rewrite>
            <rules> 
                <rule name="Force HTTPS" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

 

And your done, when you hit save. The IIS will restart if the web.config is modified, so the new rule is now enabled!

kick it on DotNetKicks.com Shout it

Pin on pinterest Plus on Googleplus Post on LinkedIn

Comments

Comment by DotNetKicks.com

Asp.Net switch site to SSL (https) the easy way

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Comment by DotNetShoutout

Asp.Net switch site to SSL (https) the easy way

Thank you for submitting this cool story - Trackback from DotNetShoutout

Comment by syngu.com

Pingback from syngu.com

Asp.Net switch site to SSL (https) the easy way | ASP.NET and .NET | Syngu

Comment by Ryan

Hi,
Great article, however I'm having issues making it redirect for mysite.com (without www.) Any idea how I can change this to work for both www and non-www. Thanks!

Ryan
Comment by JP Hellemons

Hi Ryan, try:

<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.jphellemons\.nl$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.jphellemons.nl/{R:1}" />
</rule>

JP Hellemons
Comment by Samir

This rule doesn't include directories http://localhost/myapp/Test.aspx redirects to https:///localhost/Test.aspx
Please help, how to make it include directories?

Samir