asp.net web页面 实现301重定向
2017-05-03 16:44
459 查看
首先在web.config里面 httpModules 下 添加
接下来新建 RedirectNewDomain 类,继承接口 IHttpModule
<httpModules> <add name="RedirectNewDomain" type="WZX.WebSite.Common.RedirectNewDomain, WZX.WebSite.Common" /> </httpModules>
接下来新建 RedirectNewDomain 类,继承接口 IHttpModule
public class RedirectNewDomain : IHttpModule { public void Dispose() { } public void Init(HttpApplication context) { context.AuthorizeRequest += (new EventHandler(Process301)); } public void Process301(object sender, EventArgs e) { bool flag301 = ConvertHelper.ToType<bool>(SettingHelper.Settings["flag301"], false); //读取配置文件中是否开启301 if (flag301) { HttpApplication app = (HttpApplication)sender; HttpRequest request = app.Context.Request; string AbsolutePath = request.Url.AbsolutePath; if (string.IsNullOrEmpty(AbsolutePath) || AbsolutePath == "/") app.Context.RewritePath("~/new/index.aspx"); else { //string AbsolutePath = "/new/aboutBrand"; if (!AbsolutePath.Contains(".aspx") && !AbsolutePath.Contains("/new/js/") && !AbsolutePath.Contains("/new/css/") && !AbsolutePath.Contains("/new/images/")) { //string strWebURL = request.Url.Host; //string url = "http://" + strWebURL + GetPageUrl(AbsolutePath); //app.Response.StatusCode = 301; //app.Response.AddHeader("Location", url); //app.Response.End(); app.Context.RewritePath("~" + GetPageUrl(AbsolutePath)); } } } } public string GetPageUrl(string AbsolutePath) { string PageUrl = AbsolutePath; if (AbsolutePath.Contains("/introduction")) { PageUrl = "/new/aboutBrand.aspx"; } //首页 else if (AbsolutePath.Contains("/index")) { PageUrl = "/new/index.aspx"; } //新闻详情 else if (AbsolutePath.Contains("/new/newsDetail")) { PageUrl = "/new/newsDetail.aspx"; } //新闻动态-欧罗来-eurol else if (AbsolutePath.Contains("/news")) { if (AbsolutePath.Contains("/detail")) { Regex regex = new Regex(@"/news/detail_(?<productID>\d+)"); Match match = regex.Match(AbsolutePath); if (match.Success) { string productID = match.Groups["productID"].Value; PageUrl = regex.Replace(AbsolutePath, "/new/newsDetail.aspx?Id=" + productID); } else { PageUrl = "/new/news.aspx"; } } else { Regex regex = new Regex(@"/news_(?<NewType>\d+)"); Match match = regex.Match(AbsolutePath); if (match.Success) { string NewType = match.Groups["NewType"].Value; PageUrl = regex.Replace(AbsolutePath, "/new/news.aspx?NewType=" + NewType); } else { PageUrl = "/new/news.aspx"; } } } //联系我们 else if (AbsolutePath.Contains("/contact")) { PageUrl = "/new/visitUs.aspx"; } return PageUrl; } }
相关文章推荐
- asp.net web页面元素的多语言化(多国语化)实现分享
- ASP.NET jQuery 食谱23 (jQuery AJAX实现调用页面后台方法和web服务定义的方法)
- Asp.net中配置Web.config 的UrlRewrite实现伪静态页面的代码
- asp.net web页面在后台实现打开新页面并传值
- asp.net web页面元素的多语言化(多国语化)实现分享
- asp.net开发中,web页面实现页面元素的多语言化(多国语化)
- Asp.Net实现404页面与301重定向的方法
- Asp.Net实现404页面与301重定向的方法
- asp.net截屏功能实现截取web页面
- asp.net web页面在后台实现打开新页面并传值。
- 在ASP.NET这实现Web页面间数据传递的方法
- asp.net截屏功能实现截取web页面
- asp.net截屏功能实现截取web页面
- asp.net web实现页面部分打印(转)
- 在ASP.NET中跨页面实现多选
- ASP.NET中实现Web打印的方案
- 在 Asp.Net Web 应用程序中长时间装载页面时显示进度条
- ASP.net 中的页面继承实现和通用页面的工厂模式的实现
- 在 Asp.Net Web 应用程序中长时间装载页面时显示进度条 选择自 chenweionline 的 Blog
- Asp.net实现IIS控制管理(Web虚拟目录的创建及管理)