linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘网络学院网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > asp.net >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·NetBPM工作流的一个示例:请假
·Office Web Components(OWC)绘
·asp.net正则表达式语法
·asp.net 2.0 ajax中使用PopupC
·Ado.Net读取Excel常见问题总结
·数据源为空时如何让GridView显
·如何让UpdatePanel支持文件上传
·C#.Net的常见面试试题和参考答
·asp.net ajax客户端编程+jquer
·Brettle.Web.NeatUpload.dll支
·ASP.NET使用Cookie
·ASP.NET DEMO 15: 同时支持行单
·如何使IE的后退按钮无效
·如何在ASP.NET中用OWC绘制图表
·asp.net:正确判断当前用户角色
最新文章
·Ajax Control Toolkit Animati
·讨论一下类似BlogEngine内一次
·使用CSS+SiteMap+UserControl+
·Asp.net中多彩下拉框的实现
·浅谈ASP.NET的Postback
·分清ASP.NET AJAX中的Extender
·Tip:在使用AjaxControlTookit
·有关注册DataItem的一些可能被
·IIRF(Ionic's Isapi Rewrite
·asp.net 客户端回调功能的实现
·关于控件部分的看法--读Progra
·为什么在vista上做开发
·如何封装JS和CSS文件为服务器端
·岂今我看过的最强的排序算法
·设计模式学习笔记之单件模式
Google
 
url重写实现任意二级域名或多级域名
[ 作者:  加入时间:2007-12-13 15:50:28  来自:Linux联盟收集整理 ]
简要回顾: KEgLinux联盟
   修改微软的URLRewrite能够对URL进行重写,这里要求对域名进行重写,实现http://1234.abc.com/ 到http://www.abc.com/show.aspx?id=1234的重写。 KEgLinux联盟
  步骤:1、你的域名 http://www.abc.com/ 是泛解析的,并在IIS里添加了主机头为空的映射; KEgLinux联盟
   2、修改微软的URLRewriter,要改两个地方 KEgLinux联盟
   (1).BaseModuleRewriter.cs KEgLinux联盟
  protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e) KEgLinux联盟
   { KEgLinux联盟
   HttpApplication app = (HttpApplication) sender; KEgLinux联盟
   Rewrite(app.Request.Url.AbsoluteUri, app); KEgLinux联盟
   } KEgLinux联盟
  就是将 app.Request.Path 替换成了 app.Request.Url.AbsoluteUri KEgLinux联盟
   KEgLinux联盟
   (2).ModuleRewriter.cs KEgLinux联盟
   KEgLinux联盟
  for(int i = 0; i < rules.Count; i++) KEgLinux联盟
   { KEgLinux联盟
   // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory) KEgLinux联盟
   string lookFor = "^" + rules[i].LookFor + "$"; KEgLinux联盟
   KEgLinux联盟
   // Create a regex (note that IgnoreCase is set) KEgLinux联盟
   Regex re = new Regex(lookFor, RegexOptions.IgnoreCase); KEgLinux联盟
   KEgLinux联盟
   // See if a match is found KEgLinux联盟
   if (re.IsMatch(requestedPath)) KEgLinux联盟
   { KEgLinux联盟
   // match found - do any replacement needed KEgLinux联盟
   string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo)); KEgLinux联盟
   KEgLinux联盟
   // log rewriting information to the Trace object KEgLinux联盟
   app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl); KEgLinux联盟
   KEgLinux联盟
   // Rewrite the URL KEgLinux联盟
   RewriterUtils.RewriteUrl(app.Context, sendToUrl); KEgLinux联盟
   break; // exit the for loop KEgLinux联盟
   } KEgLinux联盟
   } KEgLinux联盟
  将string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$"; KEgLinux联盟
  改成了 string lookFor = "^" + rules[i].LookFor + "$"; KEgLinux联盟
   KEgLinux联盟
   完成这2处改动之后重新编译项目,将生成的dll复制到你项目的bin目录下。 KEgLinux联盟
   KEgLinux联盟
  3、再就是写web.config里的重写正则了 KEgLinux联盟
   KEgLinux联盟
  <RewriterRule> KEgLinux联盟
   <LookFor>http://(\d+)\.abc\.com</LookFor> KEgLinux联盟
   <SendTo>/show.aspx?id=$1</SendTo> KEgLinux联盟
   </RewriterRule> KEgLinux联盟
  注意: KEgLinux联盟
  问题出来了,很多人做了,实现了域名的跳转,但很多人都是转到了首页,这里不是lookfor / to /default.aspx 的问题,我自己也遇到了这样的问题,你需要在重写规则里加一个“/”在lookFor的结尾什么的规则改后成了: KEgLinux联盟
  <RewriterRule> KEgLinux联盟
   <LookFor>http://(\d+)\.abc\.com/</LookFor> KEgLinux联盟
   <SendTo>/show.aspx?id=$1</SendTo> KEgLinux联盟
   </RewriterRule> KEgLinux联盟
  再一个问题,就是修改后的重写对参数的不友好,重写后的URL如果带有参数如http://www.abc.com/news.html?id=1000,重写就返回404.下面提供重写后参数中断的解决方法: KEgLinux联盟
  方法一: KEgLinux联盟
   修改重写规则,让正则表达式接受参数: KEgLinux联盟
  <RewriterRule> KEgLinux联盟
   <LookFor>http://(\d+)\.abc\.com/news.html(.{0,})</LookFor> KEgLinux联盟
   <SendTo>/show.aspx?id=$1</SendTo> KEgLinux联盟
   </RewriterRule>然后你的各种参数都可以匹配到NEWS页面,比如ID,分页等,在页面里就可以正常使用参数了。 KEgLinux联盟
   KEgLinux联盟
  方法二: KEgLinux联盟
   修改上面的BaseModuleRewriter.cs KEgLinux联盟
  protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e) KEgLinux联盟
   { KEgLinux联盟
   HttpApplication app = (HttpApplication) sender; KEgLinux联盟
   string path = app.Request.Url.AbsoluteUri; KEgLinux联盟
   if(path.Contains("?")) KEgLinux联盟
   { KEgLinux联盟
   path = path.Split('?')[0]; KEgLinux联盟
   } KEgLinux联盟
   Rewrite(path, app); } KEgLinux联盟
  把带“?”的绝对URL拆开,只去匹配不带参数的URL。 KEgLinux联盟
   KEgLinux联盟
  方法三: KEgLinux联盟
   修改ModuleRewriter.cs KEgLinux联盟
  for(int i = 0; i < rules.Count; i++) KEgLinux联盟
   { KEgLinux联盟
   // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory) KEgLinux联盟
   string lookFor = "^" + rules[i].LookFor + "(.{0,})$"; KEgLinux联盟
   KEgLinux联盟
   // Create a regex (note that IgnoreCase is set) KEgLinux联盟
   Regex re = new Regex(lookFor, RegexOptions.IgnoreCase); KEgLinux联盟
   KEgLinux联盟
   // See if a match is found KEgLinux联盟
   if (re.IsMatch(requestedPath)) KEgLinux联盟
   { KEgLinux联盟
   // match found - do any replacement needed KEgLinux联盟
   string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo)); KEgLinux联盟
   KEgLinux联盟
   // log rewriting information to the Trace object KEgLinux联盟
   app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl); KEgLinux联盟
   KEgLinux联盟
   // Rewrite the URL KEgLinux联盟
   RewriterUtils.RewriteUrl(app.Context, sendToUrl); KEgLinux联盟
   break; // exit the for loop KEgLinux联盟
   } KEgLinux联盟
   } KEgLinux联盟
  将string lookFor = "^" + rules[i].LookFor + "$"; 改成string lookFor = "^" + rules[i].LookFor + "(.{0,})$"; KEgLinux联盟
   KEgLinux联盟
  结束 KEgLinux联盟
  ------------------------------- KEgLinux联盟
  我的项目网站:www.at0086.com 有不少二级域名比如:hotel.at0086.com ,重写的例子有www.at0086.com/cn/69973/12/ KEgLinux联盟
   KEgLinux联盟
  参考的内容你用谷歌都可以搜到,不列举了。有需要编译好的DLL的朋友联系我。我的QQ 278199993,邮箱:xiaohc@gmail.com KEgLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·Linux无法解析域名的解决办法  (2007-12-11 11:55:11)
 ·PHP域名查询代码公布  (2007-11-21 15:45:56)
 ·使用XMLHTTP制作域名查询系统  (2007-11-20 17:02:21)
 ·使用w3Sockets组件实现域名查询功能  (2007-11-20 16:59:14)
 ·域名Whois信息查询(ASP.NET+C#)  (2007-10-26 13:03:58)
 ·PHP实现跨域名Cookie  (2007-10-11 12:39:26)
 ·用c#写的asp+域名查询程序  (2007-10-09 11:56:13)
 ·Debian Linux下ADSL拨号及动态域名的使用  (2007-08-21 10:46:57)
 ·最简单快速的Apache二级域名实现方法介绍  (2007-07-26 09:36:28)
 ·Gentoo下的科迈网动态域名配置  (2007-03-27 06:13:53)