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

Redirect non-www urls to www urls

When considering SEO, either you have to stick with www versioned url or non-www URLs. Having both accessible for a domain is a bad practice.

Abhith RajanAugust 12, 2017 · 2 min read · Last Updated:

When considering SEO, either you have to stick with www prefixed url or non-www urls. Having both accessible for a domain is a bad practice.

After I setup my website, when I checked google for my site status, it was like below,

google search

If you wonder why I searched like site:mydomain on google, this will list all the search results under the domain we specified.

See the highlighted section. If we search again to see the omitted results. We will see search result like this,

search

As you can see, both the url get indexed and one is omitted since it isn’t relevant.

So what next,

You have to choose which pattern you gonna stick to. I initially thought to go with non-www but then read the article SETTING UP UMBRACO TO ALWAYS USE WWW LINKS and decided to go with www.

And to redirect non-www to www version, I added the rewrite rule as mentioned in the article. My Code look like this,

1<system.webServer>
2 <rewrite>
3 <rules>
4 <clear />
5 <rule name="Redirect Non WWW" stopProcessing="true" xdt:Transform="Insert">
6 <match url=".*" />
7 <conditions>
8 <add input="{HTTP_HOST}" pattern="^(?!www)(.*)$" />
9 </conditions>
10 <action type="Redirect" redirectType="Permanent" url="https://www.{C:1}/{R:0}" />
11 </rule>
12 </rules>
13 </rewrite>
14 </system.webServer>

Noticed the xdt:Transform=“Insert” in the line,

1<rule name="Redirect Non WWW" stopProcessing="true" xdt:Transform="Insert">

That is because I added this rule in my Web.Release.config, Which will add the rule to the Web.config automatically during publishing. The reason for which is explained in one of my article INSERTING REWRITE RULE IN RELEASE CONFIG.

Written by Abhith Rajan
Abhith Rajan is an aspiring software engineer with more than 8 years of experience and proven successful track record of delivering technology-based products and services.
Buy me a coffee

Was this helpful?

👈 This is a live react editor.

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

Related ArticlesView All

Related StoriesView All

Related Tools & ServicesView All

woorank.com

Website Review Tool & SEO Checker

Instantly review your website for free with the WooRank SEO Checker! Find issues, track keywords, identify opportunities & create reports. 14 day Free Trial.
seobility.net

SEO Checker | Test your website for free with Seobility

Instantly review your website for free with the SEO checker from Seobility and get tips for a better search engine optimization and higher rankings.