zoukankan      html  css  js  c++  java
  • url重写实现任意二级域名或多级域名(修正参数中断问题)

    简要回顾:
         修改微软的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/news.html?id=1000,重写就返回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 ,重写的例子有www.at0086.com/TsinghuaU/


    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zj1103/archive/2008/08/30/2851381.aspx

  • 相关阅读:
    疫情数据背后,聊聊数据分析平台变迁史
    华为五大专家亲述:如何转型搞 AI?
    【华为云技术分享】LwM2M协议的学习与分享
    Spring5参考指南: BeanWrapper和PropertyEditor
    Spring5参考指南: Resources
    Spring5参考指南:事件Event
    Spring5参考指南:Environment
    Spring5参考指南:JSR 330标准注解
    Spring5参考指南:组件扫描
    Spring5参考指南:基于注解的容器配置
  • 原文地址:https://www.cnblogs.com/0000/p/1600898.html
Copyright © 2011-2022 走看看