Login


Redirecting to WWW on ASP.NET 4.0 and IIS7

By Jonathan Wood on 11/29/2010 (Updated on 12/7/2010)
Language: C#VB.NET
Technology: ASP.NET
Platform: Windows
License: CPOL
Views: 12,029
Web Development » ASP.NET » General » Redirecting to WWW on ASP.NET 4.0 and IIS7

Introduction

These days, most domains work both with and without the "www" prefix. However, have links to both versions of your domain 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, your domain won't rank as high if some links use the "www" prefix and others do not.

Force WWW Prefix

It is better to have every link use exactly the same form of your domain. To this end, it is common to redirect requests for 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 some huge software package 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.

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

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

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 justify spending time on something that appears to work just fine. I assume it will work in a future version of Visual Studio.

Conclusion

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.

End-User License

Use of this article and any related source code or other files is governed by the terms and conditions of The Code Project Open License.

Author Information

Jonathan Wood

I'm a software/website developer working out of the greater Salt Lake City area in Utah. I've developed many websites including Black Belt Coder, Insider Articles, and others.