url重寫實現任意二級域名或多級域名(修正參數中斷問題)

簡要回顧:
    修改微軟的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/show.aspx?id=1234,重寫就返回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,})$";

結束
-------------------------------
我的項目網站:http://www.at0086.com/ 有不少二級域名比如:hotel.at0086.com ,重寫的例子有http://www.at0086.com/TsinghuaU/

參考的內容你用谷歌都可以搜到,不列舉了。有需要編譯好的DLL的朋友聯繫我。我的QQ 278199993,郵箱:[email protected]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章