• 用UrlRewrite实现ASP.NET二级或多级域名(完整解决方案)


    本文转自:http://blog.csdn.net/suyiming/archive/2009/02/28/3944537.aspx

    微软的URLRewrite能够对URL进行重写,但是也只能对域名之后的部分进行重写,而不能对域名进行重写,如:可将 http://www.worldbao.com/showuser.aspx?us=suyiming  重写为 http://www.worldbao.com/suyiming.aspx  但不能将 http://www.worldbao.com/show.aspx?us=suyiming 重写为  http://suyiming.worldbao.com/

          要实现这个功能,首先要做域名泛解析,去域名管理里面的域名解析中添加一个:*.worldbao.com 指向服务器ip。

    第二,重写URLRewrite里面的两个方法。

    1.BaseModuleRewriter.cs  里面的BaseModuleRewriter_AuthorizeRequest方法

     

    1. protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)   
    2.         {   
    3.             HttpApplication app = (HttpApplication) sender;   
    4.             Rewrite(app.Request.Path, app);   
    5.         }   
    1. protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)    
    2.         {    
    3.             HttpApplication app = (HttpApplication) sender;    
    4.             Rewrite(app.Request.Path, app);    
    5.         }   

    改为

    1. protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)   
    2.         {   
    3.             HttpApplication app = (HttpApplication) sender;   
    4.             Rewrite(app.Request.Url.AbsoluteUri, app);   
    5.         }   
    1. protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)    
    2.         {    
    3.             HttpApplication app = (HttpApplication) sender;    
    4.             Rewrite(app.Request.Url.AbsoluteUri, app);    
    5.         }   

     

    2, ModuleRewriter.cs  里面的 Rewrite 方法

     

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

    改为

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

    就是将

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

    改成了

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

     

    完成这过程之后将整个项目重新编译一下。

    这些都做完之后,

    在webconfig配置里面的
     <configSections>里面 添加:

    1. <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>  
    1. <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>  

    在</configSections>下面添加

    1. <RewriterConfig>  
    2.  <!-- 处理默认首页失败-->  
    3.  <Rules>  
    4.   <RewriterRule>  
    5.    <LookFor>http://www\.worldbao\.com/</LookFor>  
    6.    <SendTo>~/default.aspx</SendTo>  
    7.   </RewriterRule>  
    8.  </Rules>  
    9.  <!-- 处理默认首页失败-->  
    10.   
    11.  <!--二级域名正则-->  
    12.  <Rules>  
    13.   <RewriterRule>  
    14.    <LookFor>http://([a-zA-Z|0-9]+)\.worldbao\.com/</LookFor>  
    15.    <SendTo>/user/user.aspx?us=$1</SendTo>  
    16.   </RewriterRule>  
    17.  </Rules>  
    18.  <!--二级域名正则-->  
    19. </RewriterConfig>  
    1. <RewriterConfig>  
    2.  <!-- 处理默认首页失败-->  
    3.  <Rules>  
    4.   <RewriterRule>  
    5.    <LookFor>http://www\.worldbao\.com/</LookFor>  
    6.    <SendTo>~/default.aspx</SendTo>  
    7.   </RewriterRule>  
    8.  </Rules>  
    9.  <!-- 处理默认首页失败-->  
    10.  <!--二级域名正则-->  
    11.  <Rules>  
    12.   <RewriterRule>  
    13.    <LookFor>http://([a-zA-Z|0-9]+)\.worldbao\.com/</LookFor>  
    14.    <SendTo>/user/user.aspx?us=$1</SendTo>  
    15.   </RewriterRule>  
    16.  </Rules>  
    17.  <!--二级域名正则-->  
    18. </RewriterConfig>  

     在httpModules里面添加

    1. <httpModules>  
    2.     <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>  
    3. </httpModules>  
    1. <httpModules>  
    2.     <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>  
    3. </httpModules>  

     

    最后一步

    IIS里面添加  ASPNET_ISAPI的通配符应用程序映射

    操作方法:IIS站点属性 ->主目录 ->  配置

    点击插入按键

     

    选择或输入C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll
    取消"确认文件是否存在"前的钩. 确定

  • 相关阅读:
    c++调用win32API控制打印机打印
    php socket 通信
    [SDOI2015][BZOJ3991] 寻宝游戏|set|dfs序|虚树|树上倍增LCA
    [NOI2015][BZOJ4195] 程序自动分析|并查集|离散化
    [NOI2015][BZOJ4196] 软件包管理器|树链剖分
    [HEOI2014][BZOJ3611] 大工程|虚树|树型dp|dfs序|树上倍增LCA
    [Usaco2007 Mar][BZOJ1638] Cow Traffic 奶牛交通|动态规划
    [HDU2222]Keywords Search|AC自动机
    [POI2007][BZOJ1103] 大都市meg|dfs序|树状数组
    [Usaco2007 Dec][BZOJ1690] 奶牛的旅行|分数规划|二分|SPFA
  • 原文地址:https://www.cnblogs.com/scgw/p/1707457.html
Copyright © 2020-2023  润新知