DEV Community

Cover image for Redirect www to non www via web.config on IIS web app
Senad Meškin
Senad Meškin

Posted on

2 2

Redirect www to non www via web.config on IIS web app

All you need to do is to add a rule into the rewrite module section:

<rule name="Force non-WWW" enabled="true" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
    </conditions>
    <action type="Redirect" url="https://{C:2}{REQUEST_URI}" appendQueryString="true" redirectType="Permanent" />
</rule>
Enter fullscreen mode Exit fullscreen mode

Your rewrite section should look like this:

<rewrite>
    <rules>
        <rule name="Force non-WWW" enabled="true" stopProcessing="true">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
            </conditions>
            <action type="Redirect" url="https://{C:2}{REQUEST_URI}" appendQueryString="true" redirectType="Permanent" />
        </rule>
    </rules>
</rewrite>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay