简要回顾:
修改微软的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,邮箱:xiaohc@gmail.com
现提供下载地址:
/Files/rene1018/URLRewriter.rar
posted on 2007-12-07 10:56
xiaohc 阅读(3722)
评论(35) 编辑 收藏 网摘
发表评论
--引用--------------------------------------------------
AlphaWu: 这个重写在.Net低版本下好像会有问题,2.0.50727.42以下,2.0.50727.832这个版本就没有问题。
<a href="
http://vsorcas.net.cn" target="_new">
http://vsorcas.net.cn</a>
--------------------------------------------------------
Microsoft .NET Framework 版本:2.0.50727.42; ASP.NET 版本:2.0.50727.42 ,这是我的.net版本,好像并没有问题,而且微软的重写示例似乎在.net1.1+vs2003的环境下写的
做泛解析对服务器压力比较大,所有的文件都会先通过Rewriter走一遍,而且容易发生dns问题,尽量别用这种技术
han_xing007@163.com
我想要源程序和Dll,请邮件我好吗?
han_xing007@163.com
我想要源程序和Dll,请邮件我好吗?
建议直接使用IonicIsapiRewriter-1.2.12c
你的方案是把 hotel.at0086.com/products.aspx?id=5,后面的参数截断掉,是吧? 如果我需要把 id 的参数也传进去呢?该如何写规则,谢谢!
哦,我了解了,rewrite只是重写path,并不包含参数。
zbwangchao@163.com
谢谢楼主 发份代码吧
我也要源码和.dll 程序集 到我邮箱, 谢谢楼主.分享! ^o^
我也要源码和.dll程序集 邮箱是soundbbg@126.com 希望楼主能分享自己的技术
谢谢 学习了
19楼,
<RewriterRule>
<LookFor>http://(\d+)\.abc\.com/news.html(.{0,})</LookFor>
<SendTo>/show.aspx?id=$1</SendTo>
</RewriterRule>
后面的(.{0,})就是带着任意参数,在实际页面上用就可以了
.net 1.1可以的,MS的那个就是1.1平台下的。
www.at0086.com/cn/69973/12/
楼主.这种方式.好象用你的方法不行.
是否有其他地方需要改动?比如IIS?
地址是:[url]www.at0086.com/cn/67793/12/[/url]
我就是想要实现这样的地址呢.
麻烦楼主告知如何实现.谢谢.
mail:wanersoft@163.com
博主,我也想要 源码和.dll 程序集 谢谢博主分享!