url重写实现二级域名

本文介绍如何使用微软的URLRewrite工具实现从http://1234.abc.com/到http://www.abc.com/show.aspx?id=1234的URL重写。包括修改BaseModuleRewriter.cs与ModuleRewriter.cs文件的具体步骤,以及如何配置web.config文件中的重写规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  修改微软的URLRewrite能够对URL进行重写,这里要求对域名进行重写,实现http://1234.abc.com/ 到http://www.abc.com/show.aspx?id=1234的重写。 bCzLinux联盟
   步骤:1、你的域名 http://www.abc.com/ 是泛解析的,并在IIS里添加了主机头为空的映射; bCzLinux联盟
   2、修改微软的URLRewriter,要改两个地方 bCzLinux联盟
   (1).BaseModuleRewriter.cs bCzLinux联盟
   protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e) bCzLinux联盟
   { bCzLinux联盟
   HttpApplication app = (HttpApplication) sender; bCzLinux联盟
   Rewrite(app.Request.Url.AbsoluteUri, app); bCzLinux联盟
   } bCzLinux联盟
   就是将 app.Request.Path 替换成了 app.Request.Url.AbsoluteUri bCzLinux联盟
   bCzLinux联盟
   (2).ModuleRewriter.cs bCzLinux联盟
   bCzLinux联盟
   for(int i = 0; i < rules.Count; i++) bCzLinux联盟
   { bCzLinux联盟
   // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory) bCzLinux联盟
   string lookFor = "^" + rules[i].LookFor + "$"; bCzLinux联盟
   bCzLinux联盟
   // Create a regex (note that IgnoreCase is set) bCzLinux联盟
   Regex re = new Regex(lookFor, RegexOptions.IgnoreCase); bCzLinux联盟
   bCzLinux联盟
   // See if a match is found bCzLinux联盟
   if (re.IsMatch(requestedPath)) bCzLinux联盟
   { bCzLinux联盟
   // match found - do any replacement needed bCzLinux联盟
   string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo)); bCzLinux联盟
   bCzLinux联盟
   // log rewriting information to the Trace object bCzLinux联盟
   app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl); bCzLinux联盟
   bCzLinux联盟
   // Rewrite the URL bCzLinux联盟
   RewriterUtils.RewriteUrl(app.Context, sendToUrl); bCzLinux联盟
   break; // exit the for loop bCzLinux联盟
   } bCzLinux联盟
   } bCzLinux联盟
   将string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$"; bCzLinux联盟
   改成了 string lookFor = "^" + rules[i].LookFor + "$"; bCzLinux联盟
   bCzLinux联盟
   完成这2处改动之后重新编译项目,将生成的dll复制到你项目的bin目录下。 bCzLinux联盟
   bCzLinux联盟
   3、再就是写web.config里的重写正则了 bCzLinux联盟
   bCzLinux联盟
   <RewriterRule> bCzLinux联盟
   <LookFor>http://(/d+)/.abc/.com</LookFor> bCzLinux联盟
   <SendTo>/show.aspx?id=$1</SendTo> bCzLinux联盟
   </RewriterRule> bCzLinux联盟
   注意: bCzLinux联盟
   问题出来了,很多人做了,实现了域名的跳转,但很多人都是转到了首页,这里不是lookfor / to /default.aspx 的问题,我自己也遇到了这样的问题,你需要在重写规则里加一个“/”在lookFor的结尾什么的规则改后成了: bCzLinux联盟
   <RewriterRule> bCzLinux联盟
   <LookFor>http://(/d+)/.abc/.com/</LookFor> bCzLinux联盟
   <SendTo>/show.aspx?id=$1</SendTo> bCzLinux联盟
   </RewriterRule> bCzLinux联盟
   再一个问题,就是修改后的重写对参数的不友好,重写后的URL如果带有参数如http://www.abc.com/news.html?id=1000,重写就返回404.下面提供重写后参数中断的解决方法: bCzLinux联盟
   方法一: bCzLinux联盟
   修改重写规则,让正则表达式接受参数: bCzLinux联盟
   <RewriterRule> bCzLinux联盟
   <LookFor>http://(/d+)/.abc/.com/news.html(.{0,})</LookFor> bCzLinux联盟
   <SendTo>/show.aspx?id=$1</SendTo> bCzLinux联盟
   </RewriterRule>然后你的各种参数都可以匹配到NEWS页面,比如ID,分页等,在页面里就可以正常使用参数了。 bCzLinux联盟
   bCzLinux联盟
   方法二: bCzLinux联盟
   修改上面的BaseModuleRewriter.cs bCzLinux联盟
   protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e) bCzLinux联盟
   { bCzLinux联盟
   HttpApplication app = (HttpApplication) sender; bCzLinux联盟
   string path = app.Request.Url.AbsoluteUri; bCzLinux联盟
   if(path.Contains("?")) bCzLinux联盟
   { bCzLinux联盟
   path = path.Split('?')[0]; bCzLinux联盟
   } bCzLinux联盟
   Rewrite(path, app); } bCzLinux联盟
   把带“?”的绝对URL拆开,只去匹配不带参数的URL。 bCzLinux联盟
   bCzLinux联盟
   方法三: bCzLinux联盟
   修改ModuleRewriter.cs bCzLinux联盟
   for(int i = 0; i < rules.Count; i++) bCzLinux联盟
   { bCzLinux联盟
   // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory) bCzLinux联盟
   string lookFor = "^" + rules[i].LookFor + "(.{0,})$"; bCzLinux联盟
   bCzLinux联盟
   // Create a regex (note that IgnoreCase is set) bCzLinux联盟
   Regex re = new Regex(lookFor, RegexOptions.IgnoreCase); bCzLinux联盟
   bCzLinux联盟
   // See if a match is found bCzLinux联盟
   if (re.IsMatch(requestedPath)) bCzLinux联盟
   { bCzLinux联盟
   // match found - do any replacement needed bCzLinux联盟
   string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo)); bCzLinux联盟
   bCzLinux联盟
   // log rewriting information to the Trace object bCzLinux联盟
   app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl); bCzLinux联盟
   bCzLinux联盟
   // Rewrite the URL bCzLinux联盟
   RewriterUtils.RewriteUrl(app.Context, sendToUrl); bCzLinux联盟
   break; // exit the for loop bCzLinux联盟
   } bCzLinux联盟
   } bCzLinux联盟
   将string lookFor = "^" + rules[i].LookFor + "$"; 改成string lookFor = "^" + rules[i].LookFor + "(.{0,})$"; bCzLinux联盟

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值