ASP.NET下用URLRewriter重写二级域名
创始人
2024-07-23 13:50:08
0

这里要求对域名进行重写,实现http://1234.abc.com/ 到 ~/Defa.aspx?id=1234的重写。

***:域名

首先域名要支持泛解悉,就是域名解悉的主机名为星号*,例:*.abc.com。如下图


这样能保证你在浏览器地址栏输入任何前缀,DNS都会把它们指向到你指定的IP地址上。

 

第二:IIS设置(Win2003 + IIS 6为例)

(1)网站必须为Web服务器的默认站点,即端口号为80,主机头为空的站点。如下图所示。


该站点接收所有对该服务器的HTTP请求(其它设置为主机头的站点除外)。所以任何二级域名访问该服务器都会由该站点进行处理。

(2)另外要在站点的“通配符应用程序映射”列表中添加ASP.NET的Web请求处理程序aspnet_isapi.dll。如下图所示。


在这里的设置,是让该站点接到的所有请求都交给aspnet_isapi.dll处理。

 

第三:修改Microsoft的URLRewriter。

运行开源项目URLRewriter。这里需要修改两个地方:

(1)BaseModuleRewriter.cs类

  1.  protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)   
  2.  {   
  3.  HttpApplication app = (HttpApplication) sender;   
  4.  //Rewrite(app.Request.Path, app);   
  5. Rewrite(app.Request.Url.AbsoluteUri, app); // ## ## ## 这里修改了   
  6. }  

这里将app.Request.Path 替换成了 app.Request.Url.AbsoluteUri

(2)ModuleRewriter.cs类

  1.  protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)   
  2.  {   
  3.  // log information to the Trace object.   
  4.  app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter");   
  5.  // get the configuration rules   
  6. RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;   
  7. // iterate through each rule...   
  8. for (int i = 0; i < rules.Count; i++)   
  9.  {   
  10.  // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)   
  11.  //string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";   
  12.  string lookFor = "^" + rules[i].LookFor + "$"; // ## ## ## 这里修改了   
  13. // Create a regex (note that IgnoreCase is set...)   
  14. Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);   
  15. // See if a match is found   
  16. if (re.IsMatch(requestedPath))   
  17.  {   
  18. // match found - do any replacement needed   
  19. string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));   
  20.  // log rewriting information to the Trace object   
  21.  app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);   
  22. // Rewrite the URL   
  23.  RewriterUtils.RewriteUrl(app.Context, sendToUrl);   
  24. break; // exit the for loop   
  25.  }   
  26.  }   
  27.  // Log information to the Trace object   
  28.  app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter");   
  29. }  

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

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

这两个地方修改完以后,生成项目。将项止目bin/Debug目录下的URLRewriter.dll文件Copy到我们要重写URL的项目中。

第四:配置项目

(1)在web.config文件的configSections节点下添加如下一行代码

  1.  

这里配置一个重写配置的类

(2)修改httpModules节点,在里面添加一行配置代码

  1.  

(3)在主节点configuration节点下添加路由规则,代码如下:

  1.   
  2.        
  3.        
  4.               
  5. http://(\w+).abc.com/ 
  6.  ~/Defa.aspx?id=$1 
  7.             
  8.    
  9.    
  10.    

代码里一个RewriterRule节点就是一个规则,这时只有一个,即把域名中的主机头部分做为Defa.aspx页面的id参数的值发送给Defa.aspx页面。

注意:这里LookFor节点里的http://(\w+).abc.com/不能少了***的反斜框

OK,一切完工

发布,上传到服务器,测试一下,如图

本次测试需要一个类库:URLRewriter.dll (测试版本 1.0.1495.18710)

官网下载地址:http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi

【编辑推荐】

  1. 详解ASP.NET MVC 3 beta新特性
  2. ASP.NET MVC 3让依赖注入实现得更简单
  3. 详解ASP.NET MVC 3 beta新特性
  4. ASP.NET MVC 3新特性与NuPack功能详解
  5. .NET开发人员应该关注的七个开源项目

 

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...