我使用的是asp.net 3.5和IIS 6.
我们如何自动将页面重定向http(s)://example.com/*到http(s)://www.example.com/*?
谢谢.
我用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();
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我们需要将模块添加到我们的Web.Config:
找到该部分<httpModules>中的<system.web>部分,它可能已经有其他几个条目,并添加如下内容:
<add name="SeoModule" type="MySite.Classes.SeoModule, MySite" />
Run Code Online (Sandbox Code Playgroud)
你可以在这里看到这个:
一切都在http://www.doodle.co.uk上结束
| 归档时间: |
|
| 查看次数: |
4304 次 |
| 最近记录: |