风行大地

导航

关于利用URLRewriter重写实现伪二级域名

利用URLRewriter重写实现伪二级域名

最近公司有个项目,要求将请求的参数放到放置到网址的前面

  例:原请求网址:www.abc.com?jc=dfs 改写层 dfs.abc.com,实现这种伪的二级域名。着实下了一番功夫,园里有不少大大已经写过同样的文章了。今天我就在这总结下。

第一步:下载一个URLRewriter。

  微软官方 关于URLRewriter的解释

  http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx

  进入以后可以下载源代码,当然也可以去别的地方下载别人修改过的代码。要安装,安装完毕以后,在“我的文档”→“MSDN”→“URL Rewriting in ASP.NET” →“URLRewritingCode.sln”   

第二步:要对URLRewriter里的方法重写

  这里我们要重写的,就是URLRewriter程序集下的两个文件 “BaseModuleRewriter.cs”和“ModuleRewriter.cs”

  1.BaseModuleRewriter.cs

  BaseModuleRewriter_AuthorizeRequest原方法
/// <summary>
        /// Called when the module's AuthorizeRequest event fires.
        /// </summary>
        /// <remarks>This event handler calls the <see cref="Rewrite"/> method, passing in the
        /// <b>RawUrl</b> and HttpApplication passed in via the <b>sender</b> parameter.</remarks>
        protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication) sender;
            Rewrite(app.Request.Path, app);
        }

  改为

  BaseModuleRewriter_AuthorizeRequest修改过代码
/// <summary>
        /// Called when the module's AuthorizeRequest event fires.
        /// </summary>
        /// <remarks>This event handler calls the <see cref="Rewrite"/> method, passing in the
        /// <b>RawUrl</b> and HttpApplication passed in via the <b>sender</b> parameter.</remarks>
        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

  Rewrite方法中原for循环
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) + "$";
                // 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
                }
            }

  改为

  Rewrite方法中修改后for循环
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
                }
            }

  修改完成以后,重新生成,在bin文件下找到Debug文件夹中找到URLRewriter.dll这个文件。

  在自己的项目中引用这个DLL文件

第三步:在自己的项目的web.config文件中坐下配置,就可以了

  1.在<configSections>节点下添加

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

  2.在<configuration>节点(即根目录的节点下)

    <RewriterConfig>
        <Rules>
          <RewriterRule>
            <LookFor>http://(\w+).abc.com/</LookFor>
            <SendTo>/Index.aspx?jv=$1</SendTo>
          </RewriterRule>
        </Rules>
      </RewriterConfig>

    如果有多个匹配地址可以写多个,<Rules>节点中多写几个<RewriterRule> 就可以了!
  3.在<system.web>节点下<httpModules>节点中添加

    <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />

  这样,基本就配置完毕了!

第四部:如果要自己测试,要把程序放到IIS服务器上,进行测试。要自己修改下hosts文件,写个虚假的域名指向程序!

 如果有需要Demo的,请留言!园子不让上传文件!抱歉

 

虽然这样很方便,但是就在我写文章的同时,看到一篇关于URL重写的园子里的一篇大大写的文章

链接帖出来

http://www.cnblogs.com/csky/archive/2006/08/09/urlrewrite.html

 

第二次写博客文章,虽然经常来园子,但是经常是伸手党,现在自己写文章,才知道不好写啊!

欢迎大家多多和我交流!  

 

posted on 2012-10-22 15:16  风行大地  阅读(2238)  评论(9编辑  收藏  举报