当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  url重写实现任意二级域名或多级域名(修正参数中断问题)

 url重写实现任意二级域名或多级域名(修正参数中断问题)

点击次数:25 次 发布日期:2008-11-26 10:32:30 作者:源代码网
源代码网推荐      简要回顾:
源代码网推荐   修改微软的URLRewrite能够对URL进行重写,这里要求对域名进行重写,实现http://1234.abc.com/ 到http://www.abc.com/show.aspx?id=1234的重写。
源代码网推荐  步骤:1、你的域名 http://www.abc.com/ 是泛解析的,并在IIS里添加了主机头为空的映射;
源代码网推荐   2、修改微软的URLRewriter,要改两个地方
源代码网推荐   (1).BaseModuleRewriter.cs
源代码网推荐  protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
源代码网推荐   {
源代码网推荐   HttpApplication app = (HttpApplication) sender;
源代码网推荐   Rewrite(app.Request.Url.AbsoluteUri, app);
源代码网推荐   }
源代码网推荐  就是将 app.Request.Path 替换成了 app.Request.Url.AbsoluteUri
源代码网推荐  
源代码网推荐   (2).ModuleRewriter.cs
源代码网推荐  
源代码网推荐  for(int i = 0; i < rules.Count; i++)
源代码网推荐   {
源代码网推荐   // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
源代码网推荐   string lookFor = "^" + rules[i].LookFor + "$";
源代码网推荐  
源代码网推荐   // Create a regex (note that IgnoreCase is set)
源代码网推荐   Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
源代码网推荐  
源代码网推荐   // See if a match is found
源代码网推荐   if (re.IsMatch(requestedPath))
源代码网推荐   {
源代码网推荐   // match found - do any replacement needed
源代码网推荐   string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
源代码网推荐  
源代码网推荐   // log rewriting information to the Trace object
源代码网推荐   app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
源代码网推荐  
源代码网推荐   // Rewrite the URL
源代码网推荐   RewriterUtils.RewriteUrl(app.Context, sendToUrl);
源代码网推荐   break; // exit the for loop
源代码网推荐   }
源代码网推荐   }
源代码网推荐  将string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
源代码网推荐  改成了 string lookFor = "^" + rules[i].LookFor + "$";
源代码网推荐  
源代码网推荐   完成这2处改动之后重新编译项目,将生成的dll复制到你项目的bin目录下。
源代码网推荐  
源代码网推荐  3、再就是写web.config里的重写正则了
源代码网推荐  
源代码网推荐  <RewriterRule>
源代码网推荐   <LookFor>http://(d+).abc.com</LookFor>
源代码网推荐   <SendTo>/show.aspx?id=$1</SendTo>
源代码网推荐   </RewriterRule>
源代码网推荐  注意:
源代码网推荐  问题出来了,很多人做了,实现了域名的跳转,但很多人都是转到了首页,这里不是lookfor / to /default.aspx 的问题,我自己也遇到了这样的问题,你需要在重写规则里加一个“/”在lookFor的结尾什么的规则改后成了:
源代码网推荐  <RewriterRule>
源代码网推荐   <LookFor>http://(d+).abc.com/</LookFor>
源代码网推荐   <SendTo>/show.aspx?id=$1</SendTo>
源代码网推荐   </RewriterRule>
源代码网推荐  再一个问题,就是修改后的重写对参数的不友好,重写后的URL如果带有参数如http://www.abc.com/news.html?id=1000,重写就返回404.下面提供重写后参数中断的解决方法:
源代码网推荐  方法一:
源代码网推荐   修改重写规则,让正则表达式接受参数:
源代码网推荐  <RewriterRule>
源代码网推荐   <LookFor>http://(d+).abc.com/news.html(.{0,})</LookFor>
源代码网推荐   <SendTo>/show.aspx?id=$1</SendTo>
源代码网推荐   </RewriterRule>然后你的各种参数都可以匹配到NEWS页面,比如ID,分页等,在页面里就可以正常使用参数了。
源代码网推荐  
源代码网推荐  方法二:
源代码网推荐   修改上面的BaseModuleRewriter.cs
源代码网推荐  protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
源代码网推荐   {
源代码网推荐   HttpApplication app = (HttpApplication) sender;
源代码网推荐   string path = app.Request.Url.AbsoluteUri;
源代码网推荐   if(path.Contains("?"))
源代码网推荐   {
源代码网推荐   path = path.Split("?")[0];
源代码网推荐   }
源代码网推荐   Rewrite(path, app); }
源代码网推荐  把带“?”的绝对URL拆开,只去匹配不带参数的URL。
源代码网推荐  
源代码网推荐  方法三:
源代码网推荐   修改ModuleRewriter.cs
源代码网推荐  for(int i = 0; i < rules.Count; i++)
源代码网推荐   {
源代码网推荐   // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
源代码网推荐   string lookFor = "^" + rules[i].LookFor + "(.{0,})$";
源代码网推荐  
源代码网推荐   // Create a regex (note that IgnoreCase is set)
源代码网推荐   Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
源代码网推荐  
源代码网推荐   // See if a match is found
源代码网推荐   if (re.IsMatch(requestedPath))
源代码网推荐   {
源代码网推荐   // match found - do any replacement needed
源代码网推荐   string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
源代码网推荐  
源代码网推荐   // log rewriting information to the Trace object
源代码网推荐   app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
源代码网推荐  
源代码网推荐   // Rewrite the URL
源代码网推荐   RewriterUtils.RewriteUrl(app.Context, sendToUrl);
源代码网推荐   break; // exit the for loop
源代码网推荐   }
源代码网推荐   }
源代码网推荐  将string lookFor = "^" + rules[i].LookFor + "$"; 改成string lookFor = "^" + rules[i].LookFor + "(.{0,})$";
源代码网推荐  
源代码网推荐  结束
源代码网推荐  -------------------------------
源代码网推荐  我的项目网站:www.at0086.com 有不少二级域名比如:hotel.at0086.com ,重写的例子有www.at0086.com/cn/69973/12/
源代码网推荐  
源代码网推荐  参考的内容你用谷歌都可以搜到,不列举了。有需要编译好的DLL的朋友联系我。我的QQ 278199993,邮箱:xiaohc@gmail.com
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华