asp.net – IIS 6如何从http://example.com/*重定向到http://www
发布时间:2020-12-16 00:02:18 所属栏目: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 defined
我使用的是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 MVC实现layui富文本编辑器应用
- asp.net-mvc – 输入类型=“隐藏”vs htmlhelper奇怪的问题
- asp.net – Internet Explorer 11在服务器端的检测
- 树莓派(一)系统安装
- asp.net-mvc – 适用于MVC 2 beta 2的MicrosoftMvcJQueryVa
- 在ASP.NET Core中相关的ConfigureAwait(false)?
- asp.net-mvc – ASP.Net Core中的动态路由
- asp.net – “localhost”上运行网站是非常慢
- asp.net – IIS 6如何从http://example.com/*重定向到http:
- asp.net – 如何从搜索引擎中完全隐藏网站?
推荐文章
站长推荐
- .NetCore技术研究-EntityFramework Core 3.0 Pre
- asp.net – Orchard CMS高级主题
- asp.net-mvc – 为什么ASP.NET MVC默认的模型绑定
- IIS 7中为ASP.NET缺少MIME类型404.17
- [WCF 4.0新特性] 标准终结点与无(.SVC)文件服务激
- asp.net-mvc – Moq的意外验证行为
- 什么用作asp.net会员后端? RavenDB或SqlServer
- ASP.NET hostingEnvironment / shadowCopyBinAss
- asp.net – 在ashx文件中,我可以设置返回文件的文
- asp.net – 将单选按钮与相应的标签对齐
热点阅读