IIS 6如何从http://example.com/*重定向到http://www.example.com/* [英] IIS 6 how to redirect from http://example.com/* to http://www.example.com/*
本文介绍了IIS 6如何从http://example.com/*重定向到http://www.example.com/*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用asp.net 3.5和IIS 6。
I am using asp.net 3.5 and IIS 6.
我们怎样才能自动重定向从 HTTP(S)网页://example.com/*
到 HTTP(S):// WWW .example.com的/ *
?
How can we automatically redirect pages from http(s)://example.com/*
to http(s)://www.example.com/*
?
感谢。
推荐答案
我这样做是有一个HttpModule:
I did this with an HttpModule:
namespace MySite.Classes
{
public class SeoModule : IHttpModule
{
// As this is defined in DEV and Production, I store the host domain in
// the web.config: <add key="HostDomain" value="www.example.com" />
private readonly string m_Domain =
WebConfigurationManager.AppSettings["HostDomain"];
#region IHttpModule Members
public void Dispose()
{
//clean-up code here.
}
public void Init(HttpApplication context)
{
// We want this fire as every request starts.
context.BeginRequest += OnBeginRequest;
}
#endregion
private void OnBeginRequest(object source, EventArgs e)
{
var application = (HttpApplication) source;
HttpContext context = application.Context;
string host = context.Request.Url.Host;
if (!string.IsNullOrEmpty(m_Domain))
{
if (host != m_Domain)
{
// This will honour ports, SSL, querystrings, etc
string newUrl =
context.Request.Url.AbsoluteUri.Replace(host, m_Domain);
// We would prefer a permanent redirect, so need to generate
// the headers ourselves. Note that ASP.NET 4.0 will introduce
// Response.PermanentRedirect
context.Response.StatusCode = 301;
context.Response.StatusDescription = "Moved Permanently";
context.Response.RedirectLocation = newUrl;
context.Response.End();
}
}
}
}
}
然后,我们需要的模块添加到我们的Web.Config:
Then we need to add the module to our Web.Config:
找到段&LT;的HttpModules&GT;
在&LT;的System.Web&GT;
部分,它很可能有一对夫妇在那里的其他条目已经,并加入类似:
Find the section <httpModules>
in the <system.web>
section, it may well have a couple of other entries in there already, and add something like:
<add name="SeoModule" type="MySite.Classes.SeoModule, MySite" />
您可以在这里的行动看到这一点:
You can see this in action here:
- http://doodle.co.uk
- http://doodlegraphics.co.uk
- http://www.doodle-graphics.co.uk
这篇关于IIS 6如何从http://example.com/*重定向到http://www.example.com/*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文