.Net中UrlRewrite实现二级或多级域名

微软的URLRewrite能够对URL进行重写,但是只能对域名之后的部分进行重写,而不能对域名进行重写,如:可将 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。 

要实现这个功能,前提条件就是  www.abc.com 是泛解析的,再就是要修改一下URLRewriter了。 
总共要修改2个文件 

1.BaseModuleRewriter.cs 

protected   virtual   void  BaseModuleRewriter_AuthorizeRequest( object  sender, EventArgs e) 
        

            HttpApplication app 
= (HttpApplication) sender; 
            Rewrite(app.Request.Path, app); 
        }

改为 

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 = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, 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 
                }
 
            }

改为 

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目录下。 

再就是写web.config里的重写正则了 

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


现在你在IE地址栏输入 http://1234.abc.com,就可以看到http://www.abc.com/show.aspx?id=1234了 

转载于:https://www.cnblogs.com/hierce/archive/2009/03/31/1425820.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值