Quantcast
Channel: Developer Blog - ASP.NET
Viewing all articles
Browse latest Browse all 10

Redirecting to WWW on ASP.NET 4.0 and IIS7

$
0
0

NOTE: This article has been updated and moved to: Redirecting to WWW on ASP.NET 4.0 and IIS7.

These days, most domains work both with and without the "www" prefix. However, this can actually hurt your ranking on search engines like Google.

The problem is that search engines like Google consider domain.com to be a different domain than www.domain.com. So as you try and build your search-engine ranking by getting more and more links to your domain, it waters down those links if some use the "www" prefix and others do not.

It is better to have every link use exactly the same form of your domain. To this end, it is common to redirect requests to domain.com to www.domain.com. If someone leaves off the prefix, the redirect will cause their browser to add it. And any links saved and published will use the form of the domain with the prefix.

I recently needed to implement this redirect for a couple of ASP.NET 4.0/IIS 7 sites but ran into a little difficulty. At first, it was recommended that I edit the .htaccess file, but that simply did not work. Apparently, it only works on older versions.

Next, I was told to download IIS 7 Remote Manager and make the changes there. Well, I assume that would work, but downloading, installing, and learning how to use a very large program just to perform a simple redirect seemed like overkill, to put it mildly.

If your site uses ASP.NET 4.0 and IIS 7, or later, there is a fairly painless way to redirect requests to the "www" version of your site by simply editing the web.config file.

Listing 1 shows the text that needs to be added somewhere within your <configuration> section.

<system.webServer>
<rewrite>
<rules>
<clear/>
<rulename="WWW Rewrite"enabled="true">
<matchurl="(.*)"/>
<conditions>
<addinput="{HTTP_HOST}"negate="true"
pattern="^www\.([.a-zA-Z0-9]+)$"/>
</conditions>
<actiontype="Redirect"url="http://www.{HTTP_HOST}/{R:0}"
appendQueryString="true"redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
<system.webServer>

Listing 1: Web.config setting to redirect domain.com to www.domain.com

Note that you will most likely see squiggly lines under the <rewrite> tag with a message that the tag is invalid. I got this message but, in fact, it worked just fine.

I found some information on the web about why I got the error but just haven't yet been able to spend time with something that appears to work just fine.

I'm a developer and this is a little out of my area of expertise so some other folks may be able to shed more light on the details of this approach. Still, if anyone comes at it from the point of view that I had, this is a great solution to look into to perform an important SEO function without digging into the depths of IIS.


Viewing all articles
Browse latest Browse all 10

Latest Images





Latest Images