SEO Canonical URLs And 301 Redirects In Windows IIS 6, IIS 7

Some readers have emailed me asking why I’m only writing about canonical URL and redirect issues for the apache/linux platform and haven’t given any advice on how to fix these issues on Microsoft Windows IIS/ASP.NET servers. So in the interest of equal time, I figure I had better present fixes for both old and new versions of IIS. In IIS 6 it can be corrected with global.asax, but with IIS 7 Microsoft added URL redirect support to the web.config file.  First a URL redirect fix for the older versions of ASP.NET on IIS 6: 

 

Canonical URL Issues In Microsoft Windows IIS 6

IIS 6 (Microsoft’s Internet Information Server webserver) doesn’t have URL redirection built-in, but you can still add it on your own. Here’s the quickest and easiest way to fix the most common canonical URL issue, where both of these URLs return the same (duplicate) content:

http://yoursite.com
http://www.yoursite.com

The quickest and easiest way I’ve found to correct this is to add the following code to your global.asax file. The global.asax file is the ASP.NET application file which gets executed before anything else every time a page is requested on your webserver. Just edit the following code and replace “yoursite.com” with, well, yoursite.com.

Sub Application_BeginRequest(ByVal sender as Object, ByVal e as EventArgs)

Try
	Dim requestedDomain As String = HttpContext.Current.Request.Url.ToString().toLower()

	If InStr(requestedDomain, "http://yoursite.com") Then

		requestedDomain = requestedDomain.Replace("http://yoursite.com", "http://www.yoursite.com")

		Response.Clear()
		Response.Status = "301 Moved Permanently"
		Response.AddHeader("Location", requestedDomain)
		Response.End()

	End If

Catch ex As Exception
	Response.Write("Error in Global.asax :" & ex.Message)
End Try

End Sub

The first thing this code will do is assign the requested URL to a string called requestedDomain.  I then use an If … Then conditional to check and see if the string contains http://yoursite.com.  If it does, I simply replace it with the www version.  Next I present a 301 Redirect header to the visitor (which may be a search engine, such as Google) and redirect the request to the new URL, which is the www version.  I also use some simple Try … Catch error checking; you can use something more complex if you wish.

 

Canonical URLs And 301 Redirects With IIS 7

With the latest release of Microsoft Windows IIS 7 webserver, URL redirect rules have been enabled in the web.config file. The following code comes from Carlos Aguilar Mares’ blog on iis.net, and demonstrates how to redirect using the new ASP.NET web.config rules. (His website also includes other code samples to demonstrate using these rules to create search engine friendly URLs, and more - check it out).

UPDATE: Pageoneresults pointed out to me that iis.net isn’t even using this technique … they didn’t fix their own canonical URL issues!!

And thanks to Dave Lawlor for reminding me that the URL rewrite module is not included by default; download links are available on iis.net

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^yoursite.com$" />
          </conditions>
          <action type="Redirect" url="http://www.yoursite.com/{R:0}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

It is important to note both of these code samples demonstrate how to redirect incoming requests from http://yoursite.com to http://www.yoursite.com. If you want to make http://yoursite.com the canonical URL, you’ll have to reverse the logic.

Now go out and bombard the interweb with my self-whoring social links:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Ma.gnolia
  • MisterWong
  • StumbleUpon
  • Technorati
  • LinkaGoGo

Tags: 301 redirect, asp.net, canonical urls, IIS, Microsoft Windows, search engines, seo, Web Servers

Similar Posts:

Comments

11 Responses to “SEO Canonical URLs And 301 Redirects In Windows IIS 6, IIS 7”

  1. Dave Lawlor Dave Lawlor on October 10th, 2008 1:11 pm

    Good article, but you might want to mention that on IIS 7 they need to download and enable the URL Rewrite module for that piece of code to work. It is not enabled with IIS 7 be default, nor does it even come included with Server 2008. You can get the module download at:

    http://learn.iis.net/page.aspx/460/using-url-rewrite-module/

    Dave

  2. Barry Wise Barry Wise on October 10th, 2008 1:12 pm

    Dave;
    Thanks for that, I should have mentioned it!

  3. Edward Beckett Edward Beckett on October 14th, 2008 3:54 am

    Barry … Excellent post - I find the content on your site … Superb.

    You are now aggregated on SEOMasterList.

    Have a nice day …

  4. John John on October 15th, 2008 12:21 pm

    Yes the modules are great on iis.net page
    these modules is very new to me so i have a question.
    The second code from Dave’s link is it a redirection module? please give us some more posts on that barry

  5. Barry Wise Barry Wise on October 15th, 2008 12:23 pm

    @John Yes, that is the IIS 7 redirect module download page link.

  6. iis iis on October 17th, 2008 3:01 pm

    Community Links 10/11/2008: URL Rewrite, ASP.NET, Extensibility, Diagnostics, WordPress…

    Here are a few cool links I found today while catching up on my IIS reading:   URL Rewrite In case…

  7. Tudor Tudor on October 18th, 2008 9:07 am

    I didn’t know about this new IIS7 feature. Right now I’m redirecting by changing headers like this:

    protected void Redirect301(string url)
    {
    Response.Status=”301 Moved Permanently”;
    Response.AddHeader(”Location”,url);
    Response.End();
    }

    I put that in a BasePage class that all pages inherit so it’s accessible everywhere. Your solution is better though :)

    By the way do you think it’s wise to put a 301 redirect on the root of a website to redirect to different pages depending on the visitor location (geoip)? I mean SEO wise…

  8. Dave Dave on November 1st, 2008 3:48 pm

    Thank you for posting this information. I am having a problem getting the global.asax file solution to work.

    I am not quite sure about a lot of things concerning this file.

    “The quickest and easiest way I’ve found to correct this is to add the following code to your global.asax file. The global.asax file is the ASP.NET application file which gets executed before anything else every time a page is requested on your webserver. Just edit the following code and replace “yoursite.com” with, well, yoursite.com.”

    Here is what I was able to find out/try….
    -asp.net is running on my server; this doesn’t appear to be a problem
    -I have never used a global.asax file; there does not appear to be one anywhere on my server
    -I was able to find a little bit of information about asp.net and global.asax files on the web. The hour or so reading up on this told me enough to have a vague idea of what I should try (see the next point) but I am clearly in over my head here.
    -I made a file with the code you provided, named it global.asap, and placed it in my www virtual root folder (there are a few websites running on this server) and this did not seem to have any effect.
    -I moved the file to the root of the website I wanted this redirect to work for, still no effect

    I can’t imagine I am the only ignorant :) person running an ISS6 webserver that is blissfully unaware of how to do all but the most simply things (= if it isn’t in IIS manager GUI, I’m getting into dangerous waters).

    Is there a simply answer to where I should put this file? Does there need to be some other basic code for the global.asax file besides the redirect? Is there something else that I need to do/know about asp.net to get this to work?

    No worries if there is no simple answer. If that is the best answer to my question, and getting into setting up asp.net is not easy, I would understand. While it would be nice to know how to fix this problem that you address in your post but it is also true that if I want to do better with my server/website management I should also have more technical training.

    Thanks.

  9. Barry Wise Barry Wise on November 3rd, 2008 1:12 pm

    Dave;
    That is a question better answered in a forum on IIS issues, since there are so many variables at play and since I have no idea how the rest of your server is configured.

  10. anchal@SEO seomega corp anchal@SEO seomega corp on November 25th, 2008 11:07 am

    Thanks Barry..
    this redirection code is really very helpful for me.

  11. SEO Strategies SEO Strategies on December 17th, 2008 8:10 am

    Great code, thanks for that It will be useful for my all sites.

Leave a Reply