Redirect WWW to Non-WWW using Web.Config in IIS

Code first, talks later.

        <rule name="Redirect to non-www" stopProcessing="true">
          <match url="(.*)" negate="false"></match>
          <action type="Redirect" url="{R:1}"></action>
            <add input="{HTTP_HOST}" pattern="^yourdomain\.com$" negate="true"></add>

Replace the "yourdomain" with your domain name and add it under the system.webServer section in the Web.Config, and that's it.

Some people prefer www domain and some non-www. I personally prefer www URLs. One important point to remember is never to allow both accessible at the same time. Read my article REDIRECT NON-WWW URLS TO WWW URLS to know why. It will be better if you add the rule on the Web.Release.config with a slight modification. Read more about it on my article INSERTING REWRITE RULE IN RELEASE CONFIG.

comments powered by Disqus

About the author

  • Abhith Rajan

    Abhith Rajan

    Abhith Rajan is a full-stack developer, consultant, Sysberries employee, and biker. Who passionate about C# and JavaScript. Works on his blog, his personal finance web app and its react native mobile app in his free time.

    Buy me a coffeeBuy me a coffee