Pete Freitag Pete Freitag

Redirect www and non https in IIS using web.config

Published on June 19, 2019
By Pete Freitag

Had the need to setup a web.config file in IIS that would redirect ALL http (non https) and requests for to the non-www

Normally I would just setup a second website in IIS to handle this, but this approach has the advantage of being portable (move between servers), and you can also keep this here as a backup even if you do use a second site for the redirect.

So here an example IIS Rewrite Module rule to be placed in web.config to redirect anything other than to the https non-www domain origin:

<rule name="HTTPSCanonicalHostNameRule" enabled="true" stopProcessing="true">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
                <add input="{HTTPS}" pattern="off" />
            <action type="Redirect" url="{R:1}" appendQueryString="true" />

And here is where you put that block within the web.config file:

<?xml version="1.0" encoding="UTF-8"?>
                <!-- put rule here -->

iis https

Redirect www and non https in IIS using web.config was first published on June 19, 2019.

Discuss / Follow me on Twitter ↯