天天看點

【微軟的URLRewriter】url重寫實作任意二級域名或多級域名

簡要回顧: 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聯盟

  -------------------------------

繼續閱讀