iis | seo | web-config | rewrite-rule | aspnet

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

Use IIS rewrite rule to redirect (301) all www requests to non-www.

Abhith Rajan
Abhith RajanSeptember 19, 2017 · 1 min read · Last Updated:

Code first, talks later.

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

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.

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub


Abhith Rajan

Written byAbhith Rajan
Abhith Rajan is a software engineer by day and a full-stack developer by night. He's coding for almost a decade now. He codes 🧑‍💻, write ✍️, learn 📖 and advocate 👍.
Connect

Is this page helpful?

Related ArticlesView All

Related StoriesView All

Related Tools & ServicesView All

metatags.io

Meta Tags — Preview, Edit and Generate

With Meta Tags you can edit and experiment with your content then preview how your webpage will look on Google, Facebook, Twitter and more!
en.ryte.com

Robots.txt generator - Create a robots.txt file instantly

Generate a robots.txt file for free with this easy-to-use tool. Make sure search engine crawlers are crawling and indexing your site.