SEO Canonical URLs And 301 Redirects In Windows IIS 6, IIS 7
October 9, 2008 | Barry Wise | Code Samples, Search Engine Optimization
25 Comments
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.
Tags: 301 redirect, asp.net, canonical urls, IIS, Microsoft Windows, search engines, seo, Web Servers
Similar Posts:
- SEO Search Engine Friendly URLs with ASP.NET and IIS
- Using IIS URL Rewrite Extensions to Redirect Default.aspx
- How to Create 404 Page Not Found Custom Error Pages in PHP
- SEO Issues with Duplicate Content: Htaccess, Robots and URLs – Part 2
- Global Find and Replace In WordPress using MySQL
Comments
25 Responses to “SEO Canonical URLs And 301 Redirects In Windows IIS 6, IIS 7”
Leave a Reply























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
Dave;
Thanks for that, I should have mentioned it!
Barry … Excellent post – I find the content on your site … Superb.
You are now aggregated on SEOMasterList.
Have a nice day …
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
@John Yes, that is the IIS 7 redirect module download page link.
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…
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…
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.
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.
Thanks Barry..
this redirection code is really very helpful for me.
Great code, thanks for that It will be useful for my all sites.
thanks for your article.
Thanks for the Article Barry,
Is there a reason to use the global.asax file over the 301 redirection available in IIS 6.0 when configuring the home directory?
Would you use the global.asax file because some search engines treat this 301 as a 302? Or is it an issue of load on IIS.
Thanks!
Matt;
You could use a regular 301 rule. The reason I use the global.asax file is because I also perform URL re-writing to create search engine friendly URLs. See other post about that here: http://www.barrywise.com/2008/11/seo-search-engine-friendly-urls-with-aspnet-and-iis/
Great stuff barry, I predominatly work with Apache but this has been a great insight to how this is done in IIS
Great tips!
I always used Redirects in .htaccess files or setting up the server.
Thanks for the info.
I´ve already bookmarked and subscribed the feeds.
Issues like this is why I use Appache. Thx 4 the info
One of the easiest ways to redirect using IIS is to just create a new web site. Most people are interested in making the http://www.yoursite.com domain the one that answers canonical URLs. Using IIS headers, simply remove the yoursite.com domain from the primary web site.
Create a new web site and make sure during the setup that you choose the path that your site’s files are located. Once the site is setup, open its properties, click the Advanced button and enter yourdomain.com as the header. Click the Home Directory tab and check the radio “A redirection to a URL” and the checkbox “A permanent redirection for this resource”.
All the files in your website will now automatically and permanently redirect to http://www.yourdomain.com.
I can see times when this could cause issues with web sites. For instance, if the developers have hard-coded yourdomain.com into the web code and the code misbehaves during the redirect. Albeit, if you’re coding like that you should have your hand slapped like a Catholic nun with a ruler, it can happen. From my experience, if this doesn’t work for your web site you have other issues that should be dealt with anyway because things aren’t being done right somewhere in the code.
Again, easy fix. Each situation will be unique and should be assessed thoroughly before implementing a solution. Sometimes the simple solutions are the best; sometimes developers need a swift kick in the pants.
Cheers!
Thanks for this. I just inherited a site on IIS 6. I’ve only worked with Apache and .htaccess for 301s in the past.
My question is, I have also found a tutorial that suggests 301 is best done through IIS 6 control panel rather than through the global.asax file.
We have an outsourced network admin who knows nothing of SEO and so I need help figuring out the right way to 301 in IIS 6.
Sorry for asking a potentially dumb question.
Hi Barry,
Thanks for the article. I’ve implemented your IIS 6 solution on my site. It works well, except that if I type in http://mysite.com/ it returns http://www.mysite.com/index.html rather than simply http://www.mysite.com/ (i.e. I don’t want to have the “index.html” portion visible in the address).
Is there some additional code that can be added to the global.asax file to handle this?
Thanks!
I did as you wrote but have got the error: Error in Global.asax: followed by no error message. Wonder where I’ve gone wrong?
If I understand correctly he said you can use web.config instead of the global.asax on IIS7. I tried implementing the changes in web.config to sort canonicalization for SEO and worked fine for me.
Thanks,
Mark
Thanks for this as have been trying to find the fianl and correct way for doing this. My host said they could not do it and I needed to do it in code, this is a great clean example of a way to do as this was the last of my canonicalization mission to resolve on my site.
Thanks Barry – Good Post mate
Great stuff barry, I predominatly work with Apache but this has been a great insight to how this is done in IIS
I have used IIS 7 url rewriting for Canonical problem, its working but i have a master page on which i have placed a login control and when i tried to login it simply refreshes the page and nothing happend on default page. but when do the same thing from other page login works.
I removed the url rule then check and its working again, i m getting the problem why its not working on defalut.aspx page.
There is also a button on master page.On clicking that button i just passed some querystring and redirect to other page and it also not working just refreshes the page.
After removing the canonical rule all is well again.
Remember only on default.aspx page this creates a problem except this page everything works fine on other pages.Please help me to solve this issue