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.

commit 4a19c74 · authored · 2 min read

Redirect non-www urls to www urls

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
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
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,

<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect Non WWW" stopProcessing="true" xdt:Transform="Insert">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(.*)$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://www.{C:1}/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>

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

<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.

local graph · open full graph

"Redirect non-www urls to www urls" is connected to the topics Internet Information Services, Search Engine Optimization, Web.config, Rewrite Rules, ASP.NET and related to 6 other entries. Redirect HTTPS requests to HTTP using IIS Rewrite rule in Web.Config Redirect HTTPS requests to … Redirect HTTP to HTTPS using Web.Config in IIS Redirect HTTP to HTTPS usin… Redirect WWW to Non-WWW using Web.Config in IIS Redirect WWW to Non-WWW usi… C# - Get Web.Config Connection String in a Class Library C# - Get Web.Config Connect… IIS - Disable CORS IIS - Disable CORS IIS - OPTIONS Requests Returns 404 IIS - OPTIONS Requests Retu… Internet Information Services #iis Search Engine Optimization #seo Web.config #web-config Rewrite Rules #rewrite-rule ASP.NET #aspnet Redirect non-www urls to www urls

comments

loading discussion…