ASP.NET中實現二級或多級域名(修改UrlRewrite)

本文轉自:http://www.cnblogs.com/jzywh/archive/2005/09/29/246650.Html
 
大家應該知道,微軟的URLRewrite能夠對URL進行重寫,但是也只能對域名之後的部分進行重寫,而不能對域名進行重寫,如:可將 http://http://www.abc.com//1234/  重寫爲 http://www.abc.com/show.aspx?id=1234  但不能將
http://1234.abc.com/  重寫爲  http://www.abc.com/show.aspx?id=1234

要實現這個功能,前提條件就是  http://www.abc.com/ 是泛解析的,再就是要修改一下URLRewriter了。
總共要修改2個文件

1.BaseModuleRewriter.cs

None.gifprotected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
ExpandedBlockStart.gif        
{
InBlock.gif            HttpApplication app 
= (HttpApplication) sender;
InBlock.gif            Rewrite(app.Request.Path, app);
ExpandedBlockEnd.gif        }

改爲

None.gifprotected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
ExpandedBlockStart.gif        
{
InBlock.gif            HttpApplication app 
= (HttpApplication) sender;
InBlock.gif            Rewrite(app.Request.Url.AbsoluteUri, app);
ExpandedBlockEnd.gif        }


就是將  app.Request.Path 替換成了  app.Request.Url.AbsoluteUri
<SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </SCRIPT>
2.ModuleRewriter.cs

None.giffor(int i = 0; i < rules.Count; i++)
ExpandedBlockStart.gif            
{
InBlock.gif                
// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
InBlock.gif
                string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
InBlock.gif
InBlock.gif                
// Create a regex (note that IgnoreCase is setdot.gif)
InBlock.gif
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
InBlock.gif
InBlock.gif                
// See if a match is found
InBlock.gif
                if (re.IsMatch(requestedPath))
ExpandedSubBlockStart.gif                
{
InBlock.gif                    
// match found - do any replacement needed
InBlock.gif
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
InBlock.gif
InBlock.gif                    
// log rewriting information to the Trace object
InBlock.gif
                    app.Context.Trace.Write("ModuleRewriter""Rewriting URL to " + sendToUrl);
InBlock.gif
InBlock.gif                    
// Rewrite the URL
InBlock.gif
                    RewriterUtils.RewriteUrl(app.Context, sendToUrl);
InBlock.gif                    
break;        // exit the for loop
ExpandedSubBlockEnd.gif
                }

ExpandedBlockEnd.gif            }

改爲

None.giffor(int i = 0; i < rules.Count; i++)
ExpandedBlockStart.gif            
{
InBlock.gif                
// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
InBlock.gif
                string lookFor = "^" + rules[i].LookFor + "$";
InBlock.gif
InBlock.gif                
// Create a regex (note that IgnoreCase is setdot.gif)
InBlock.gif
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
InBlock.gif
InBlock.gif                
// See if a match is found
InBlock.gif
                if (re.IsMatch(requestedPath))
ExpandedSubBlockStart.gif                
{
InBlock.gif                    
// match found - do any replacement needed
InBlock.gif
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
InBlock.gif
InBlock.gif                    
// log rewriting information to the Trace object
InBlock.gif
                    app.Context.Trace.Write("ModuleRewriter""Rewriting URL to " + sendToUrl);
InBlock.gif
InBlock.gif                    
// Rewrite the URL
InBlock.gif
                    RewriterUtils.RewriteUrl(app.Context, sendToUrl);
InBlock.gif                    
break;        // exit the for loop
ExpandedSubBlockEnd.gif
                }

ExpandedBlockEnd.gif            }



string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";

改成了

string lookFor = "^" + rules[i].LookFor + "$";


完成這2處改動之後重新編譯項目,將生成的dll複製到bin目錄下。

再就是寫web.config裏的重寫正則了

None.gif<RewriterRule>
None.gif            
<LookFor>http://(/d+)/.abc/.com/</LookFor>
None.gif            
<SendTo>/show.aspx?id=$1</SendTo>
None.gif        
</RewriterRule>


好了大功告成,你在IE地址欄輸入http://1234.abc.com/,就可以看到http://www.abc.com/show.aspx?id=1234

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