asp.net – IIS 6如何从http://example.com/*重定向到http://www.example
发布时间:2020-05-23 22:42:13 所属栏目:asp.Net 来源:互联网
导读:我使用的是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
|
我使用的是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();
}
}
}
}
}
然后我们需要将模块添加到我们的Web.Config: 找到< httpModules>部分在< system.web>中部分,它可能已经有几个其他条目,并添加如下: <add name="SEOModule" type="MySite.Classes.SEOModule,MySite" /> 你可以在这里看到这个: > http://doodle.co.uk 一切都在http://www.doodle.co.uk结束 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 程序或函数期望未提供的参数
- asp.net-mvc – 如何在MVC6或AspNet Core或IdentityCore中更
- ASP.NET MVC2与实体框架4 – AsEnumerable()或ToList()在存
- 远程服务器返回错误:(401)未经授权.在ASP.NET中使用CSOM
- asp.net – 在MVC和WebForms之间共享一个主 – 处理
- asp.net-mvc – 如何将MEF与ASP.NET MVC 4和ASP.NET Web AP
- asp.net-mvc-3 – 为什么@ Html.Label()删除一些字符
- ide – 我如何处理必须编写经典ASP的代码?
- asp.net-web-api – 仅在第一个Web请求(WebAPI 2,OWIN 3,Ni
- asp.net-mvc – 在kendo模板中渲染剑道控件(Razor)
推荐文章
站长推荐
- asp.net-web-api – 如何使用OWIN自动主机的web
- 这个ASP.NET功能在哪里记录? %= string format
- asp.net – TeamCity可以使用sln2008构建运行程序
- Block内的ASP.NET服务器端注释
- 如何设置特定于ASP.NET请求的log4net上下文属性?
- 使用ASP.net MVCJQuery将HTML标签(代码)作为字符
- remoting和webservice有什么区别
- asp.net-mvc – ASP.NET MVC身份默认实现
- asp.net-mvc – ASP.NET MVC – AntiForgeryToke
- asp.net-mvc-routing – ASP.NET MVC区域:如何隐
热点阅读
