.net_ url重写 (修改URLReWriter) 实现 任意 动态 二级 域名

 

http://1234.eshinfo.com/ 重写为 http://www.eshinfo.com/show.aspx?id=1234

要实现这个功能,
前提条件就是
http://www.eshinfo.com/ 是泛解析的(添加一个*.kerry.com的二级域名,指向你的服务器ip),
再就是要修改一下URLRewriter了,总共要修改2个文件

1.BaseModuleRewriter.cs

protected virtual void BaseModuleRewriter_AuthorizeRequest( object sender, EventArgs e)
        
{
             HttpApplication app
= (HttpApplication) sender;
             Rewrite(app.Request.Path, app); //改为 Rewrite(app.Request.Url.AbsoluteUri, app);
         }

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) + "$"; // 改为 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
                 }
             }


完成这2处改动之后重新编译项目,将生成的dll复制到bin目录下。

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

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

好了大功告成

微软的URLRewriter是什么?这个项目在哪里下载的?
这个是在msdn上一篇介绍URLRewriter的文章中提供的示例程序,可以在这里下载到
http://www.microsoft.com/china/m ... t/URLRewriting.mspx

什么是httpmodule?

简单理解,就是一块处理http请求的程序
更详细的理解,请查阅sdk文档.

怎么样实现泛解析?

首先,在域名服务商那里添加一个*.kerry.com的二级域名,指向你的服务器ip
然后,在IIS里建立一个站点,这个站点的主机头留空,一般端口是80. 这个站点就是整个服务器端口80的默认网站.
给这个站点添加一个通配符应用程序映射(IIS站点属性 ->主目录 ->   配置),这个映射的目的是要asp.net ISAPI接管任何没有在IIS里明确的二级域名站点.

随便输入二级域名的时候,发生了什么?
当IIS检测到传入的url是一个二级域名的时候,它会先检查IIS上有没有注册了这个二级域名的站点,如果有,就转入到这个站点,否则,就会转到默认站点,这个默认站点就是之前配置的主机头为空的那个站点.所以,一个端口只能有一个主机头为空的站点.
我们已经设定由asp.net ISAPI接管这些没有家的孩子.写程序,分析传入的url,执行重写.

为什么我的httpmodule好像没有起作用?

在httpmodule程序里设置断点后,无论怎么样,流程都没有从这里走.原因在于,你没有向web程序注册你的httpmodule程序.这个工作需要在web.config中完成.
<system.web>
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter"   name="ModuleRewriter" />
</httpModules>
</system.web>

为什么总是提示我”未知的配置节RewriterConfig错误”

这是因为你没有向web程序注册你的RewriterConfig配置节. 这个工作需要在web.config中完成.
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
然后,你可以在<configuration>里使用RewriterConfig节配置规则了.

url是在httpmodule的哪个部分处理的?

大多的工作是在URLRewriter. ModuleRewriter. Rewrite()方法里.关键阶段是这里:
if (re.IsMatch(requestedPath))
很明显,这个判断传入的url是否是我们要重写的url,大家接着看,
String sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules.SendTo));
这里接受到web.config中配置的要转到的目标url
RewriterUtils.RewriteUrl(app.Context, sendToUrl);
在内部把url重写.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值