现在的位置: 首页 > 综合 > 正文

【微软的URLRewriter】url重写实现任意二级域名或多级域名

2019年05月11日 ⁄ 综合 ⁄ 共 4184字 ⁄ 字号 评论关闭
简要回顾: zWdLinux联盟
   修改微软的URLRewrite能够对URL进行重写,这里要求对域名进行重写,实现http://1234.abc.com/ 到http://www.abc.com/show.aspx?id=1234的重写。 zWdLinux联盟
  步骤:1、你的域名 http://www.abc.com/ 是泛解析的,并在IIS里添加了主机头为空的映射; zWdLinux联盟
   2、修改微软的URLRewriter,要改两个地方 zWdLinux联盟
   (1).BaseModuleRewriter.cs zWdLinux联盟
  protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e) zWdLinux联盟
   { zWdLinux联盟
   HttpApplication app = (HttpApplication) sender; zWdLinux联盟
   Rewrite(app.Request.Url.AbsoluteUri, app); zWdLinux联盟
   } zWdLinux联盟
  就是将 app.Request.Path 替换成了 app.Request.Url.AbsoluteUri zWdLinux联盟
   zWdLinux联盟
   (2).ModuleRewriter.cs zWdLinux联盟
   zWdLinux联盟
  for(int i = 0; i < rules.Count; i++) zWdLinux联盟
   { zWdLinux联盟
   // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory) zWdLinux联盟
   string lookFor = "^" + rules[i].LookFor + "$"; zWdLinux联盟
   zWdLinux联盟
   // Create a regex (note that IgnoreCase is set) zWdLinux联盟
   Regex re = new Regex(lookFor, RegexOptions.IgnoreCase); zWdLinux联盟
   zWdLinux联盟
   // See if a match is found zWdLinux联盟
   if (re.IsMatch(requestedPath)) zWdLinux联盟
   { zWdLinux联盟
   // match found - do any replacement needed zWdLinux联盟
  
string sendToUrl =
RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath,
re.Replace(requestedPath, rules[i].SendTo)); zWdLinux联盟
   zWdLinux联盟
   // log rewriting information to the Trace object zWdLinux联盟
   app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl); zWdLinux联盟
   zWdLinux联盟
   // Rewrite the URL zWdLinux联盟
   RewriterUtils.RewriteUrl(app.Context, sendToUrl); zWdLinux联盟
   break; // exit the for loop zWdLinux联盟
   } zWdLinux联盟
   } zWdLinux联盟
  将string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$"; zWdLinux联盟
  改成了 string lookFor = "^" + rules[i].LookFor + "$"; zWdLinux联盟
   zWdLinux联盟
   完成这2处改动之后重新编译项目,将生成的dll复制到你项目的bin目录下。 zWdLinux联盟
   zWdLinux联盟
  3、再就是写web.config里的重写正则了 zWdLinux联盟
   zWdLinux联盟
  <RewriterRule> zWdLinux联盟
   <LookFor>http://(/d+)/.abc/.com</LookFor> zWdLinux联盟
   <SendTo>/show.aspx?id=$1</SendTo> zWdLinux联盟
   </RewriterRule> zWdLinux联盟
  注意: zWdLinux联盟
  问题出来了,很多人做了,实现了域名的跳转,但很多人都是转到了首页,这里不是lookfor / to /default.aspx 的问题,我自己也遇到了这样的问题,你需要在重写规则里加一个“/”在lookFor的结尾什么的规则改后成了: zWdLinux联盟
  <RewriterRule> zWdLinux联盟
   <LookFor>http://(/d+)/.abc/.com/</LookFor> zWdLinux联盟
   <SendTo>/show.aspx?id=$1</SendTo> zWdLinux联盟
   </RewriterRule> zWdLinux联盟
  再一个问题,就是修改后的重写对参数的不友好,重写后的URL如果带有参数如http://www.abc.com/news.html?id=1000,重写就返回404.下面提供重写后参数中断的解决方法: zWdLinux联盟
  方法一: zWdLinux联盟
   修改重写规则,让正则表达式接受参数: zWdLinux联盟
  <RewriterRule> zWdLinux联盟
   <LookFor>http://(/d+)/.abc/.com/news.html(.{0,})</LookFor> zWdLinux联盟
   <SendTo>/show.aspx?id=$1</SendTo> zWdLinux联盟
   </RewriterRule>然后你的各种参数都可以匹配到NEWS页面,比如ID,分页等,在页面里就可以正常使用参数了。 zWdLinux联盟
   zWdLinux联盟
  方法二: zWdLinux联盟
   修改上面的BaseModuleRewriter.cs zWdLinux联盟
  protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e) zWdLinux联盟
   { zWdLinux联盟
   HttpApplication app = (HttpApplication) sender; zWdLinux联盟
   string path = app.Request.Url.AbsoluteUri; zWdLinux联盟
   if(path.Contains("?")) zWdLinux联盟
   { zWdLinux联盟
   path = path.Split('?')[0]; zWdLinux联盟
   } zWdLinux联盟
   Rewrite(path, app); } zWdLinux联盟
  把带“?”的绝对URL拆开,只去匹配不带参数的URL。 zWdLinux联盟
   zWdLinux联盟
  方法三: zWdLinux联盟
   修改ModuleRewriter.cs zWdLinux联盟
  for(int i = 0; i < rules.Count; i++) zWdLinux联盟
   { zWdLinux联盟
   // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory) zWdLinux联盟
   string lookFor = "^" + rules[i].LookFor + "(.{0,})$"; zWdLinux联盟
   zWdLinux联盟
   // Create a regex (note that IgnoreCase is set) zWdLinux联盟
   Regex re = new Regex(lookFor, RegexOptions.IgnoreCase); zWdLinux联盟
   zWdLinux联盟
   // See if a match is found zWdLinux联盟
   if (re.IsMatch(requestedPath)) zWdLinux联盟
   { zWdLinux联盟
   // match found - do any replacement needed zWdLinux联盟
  
string sendToUrl =
RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath,
re.Replace(requestedPath, rules[i].SendTo)); zWdLinux联盟
   zWdLinux联盟
   // log rewriting information to the Trace object zWdLinux联盟
   app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl); zWdLinux联盟
   zWdLinux联盟
   // Rewrite the URL zWdLinux联盟
   RewriterUtils.RewriteUrl(app.Context, sendToUrl); zWdLinux联盟
   break; // exit the for loop zWdLinux联盟
   } zWdLinux联盟
   } zWdLinux联盟
  将string lookFor = "^" + rules[i].LookFor + "$"; 改成string lookFor = "^" + rules[i].LookFor + "(.{0,})$"; zWdLinux联盟
   zWdLinux联盟
  结束 zWdLinux联盟
  -------------------------------

抱歉!评论已关闭.