Pete Freitag Pete Freitag

Redirect www and non https in IIS using web.config

Published on June 19, 2019
By Pete Freitag
web

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

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 https://example.com/ 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" />
            </conditions>
            <action type="Redirect" url="https://example.com/{R:1}" appendQueryString="true" />
</rule>

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

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <!-- put rule here -->
            </rules>
        </rewrite>
    </system.webServer>
</configuration>


iis https

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


Discuss / Follow me on Twitter ↯