问题
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?
回答1:
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
回答2:
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.
回答3:
In the http redirect make sure that your redirect URL is in the right format.
Instead of double slash after http, if you put single slash it will go to indefinite loop.
ie. "http://" is correct. "http:/" will create a indefinite loop.
回答4:
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.
来源:https://stackoverflow.com/questions/1246041/setting-up-http-redirect-for-seo-in-iis7