views:

814

answers:

3

Hi,

I want all requests to http://mydomain.com to be 301 redirected to http://www.mydomain.com for SEO purposes.

In order to do this, can I use IIS7's HTTP redirect method? I tried setting the HTTP redirect to www.mydomain.com, but this led to a permanent loop.

Edit: URL Rewrite will do the job, and I am going to use it, unless somebody else has a better idea:

http://blogs.msdn.com/carlosag/archive/2008/09/02/IIS7UrlRewriteSEO.aspx

Any suggestions?

A: 

Create mydomain.com as a seperate site and have it redirect that way.

Seems like you do it like this:

http://technet.microsoft.com/en-us/library/cc732969%28WS.10%29.aspx

http://technet.microsoft.com/en-us/library/cc770393%28WS.10%29.aspx

Consider using Apache for this, much easier.

mattl
Actually, this would be a much easier and better way, as I've discovered: http://blogs.msdn.com/carlosag/archive/2008/09/02/IIS7UrlRewriteSEO.aspx
Wild Thing
+1  A: 

There probably is a way to do this with IIS7. The trick would be in providing a condition to prevent the infinite loop. Unfortunately I'm not sure how to do that exactly.

But you can also do this in .NET code very easily as I'm doing the same thing. I'd just put this in your Global.asax:

Imports System.Web.HttpContext
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
  Dim strWebsite As String = "http://www.mydomain.com"

  If Not Current.Request.Url.AbsoluteUri.StartsWith(strWebsite) Then
    Current.Response.Clear()
    Current.Response.Status = "301 Moved Permanently"
    Current.Response.AddHeader("Location", strWebsite & Current.Request.RawUrl)
    Current.Response.End()
  End If
End Sub
Steve Wortham
Of course if you have full access to your web server and can install the URL Rewrite Module you may prefer to go that route. I wish I could do that for a few of my sites actually, but I'm on a shared server so I'm somewhat limited.
Steve Wortham
Yes, I do have the URL Rewrite module now installed. Will do some research and set it up. Your other solution isn't too bad either. Sorta hackish, though.
Wild Thing
Hackish -- maybe so. It works like a charm though. ;)
Steve Wortham
A: 

Yup, the UrlRewrite module is the way to go here. Now, if you are in a scenario where you don't have IIS7 handy, or can't use the url rewrite module, you can still do this with HTTP redirects. The trick is to use two separate virtual sites. The first site listens for the example.com host header and forwards everything to www.example.com. The second listens for www.example.com and behaves normally.

Wyatt Barnett