Developer Blog
Articles about Using Microsoft Developer Tools

Redirecting to WWW on ASP.NET 4.0 and IIS7

Monday, June 14, 2010 8:23 AM by jonwood

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

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.

Tags:  
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Validation Controls Lost Their Red Color

Friday, June 04, 2010 2:27 AM by jonwood

I was recently finishing up a new website (http://www.trailcalendar.com) and I noticed that all the validation and validation summary controls in my project no longer appeared in red.

Normally, these controls will display with "color:Red" as part of their style attribute unless you override the ForeColor property. Even if you have CSS styles in effect that change the text color, the inline attribute will override it and the control will appear red. But, all of a sudden, this was no longer being included in my output.

I could not figure this one out. I created a new test project from scratch and I got the same thing (no red color). Yet, when I looked at my older projects, all validation controls were including the color style attribute. What had changed?

It turns out that what had changed is that I recently upgraded to Visual Studio 2010 and ASP.NET 4.0. It appears ASP.NET 4.0 has changed the way that it renders some of the output. Most of these changes are great as they result in more concise HTML. (Verbose HTML has always been an issue with ASP.NET.)

Some of these changes include menus, which are now output as lists instead of tables, properties like border="0" are no longer included, and error text of validation controls is no longer set to red! Note that I think these are great changes and highly recommend the upgrade. In an effort to make the HTML more concise, VS 2010 also gives you the option of preventing your tag IDs growing from something like "LinkButton1" to "ContentPlaceHolder1_ContentPlaceHolder1_Repeater1_LinkButton1_0".

So, now that I know what is causing this, what is the solution? Well, it turns out there is a compatibility setting for this. The controlRenderingCompatibilityVersion can be set to "3.5" to produce the same rendering I'm familiar with. This setting goes in your web.config file and is demonstrated in Listing 1.

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="false" targetFramework="4.0" />
    <pages controlRenderingCompatibilityVersion="3.5" />
  </system.web>
</configuration>

Listing 1: The controlRenderingCompatibilityVersion Setting.

It turns out that the Visual Studio Conversion Wizard sets this setting automatically when upgrading a project. This makes sense because you might have a big project that has many places that no longer display correctly. To be honest, I'm just not sure what happened in my case and don't recall if I ran the Conversion Wizard or not.

I'm still not entirely clear on what the expectation is here. I love the option of producing more compact HTML, but I still want my validation text to appear red and I would rather use the newer ASP.NET 4.0 rendering. On my project, I will probably go in and change the ForeColor property on every validation control in my project to "Red".

Either way, I thought I would post about this in case anyone else was bit by this issue. It certainly took me by surprise.

Tags:  
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed