大家应该知道,微软的URLRewrite能够对URL进行重写,但是也只能对域名之后的部分进行重写,而不能对域名进行重写,

如:可将 http://http://www.115sou.com/qq/  重写为 http://www.115sou.com/show.aspx?id=qq

但不能将 http://qq.115sou.com/  重写为  http://www.115sou.com/index.aspx?id=qq。

要实现这个功能,前提条件就是  http://www.115sou.com/ 是泛解析的,再就是要修改一下URLRewriter了。 
总共要修改2个文件 

1.BaseModuleRewriter.cs

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

改为

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 = "^" + 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 
                } 
            }

改为

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目录下。

再就是写web.config里的重写正则了

<RewriterRule>
            <LookFor>http://(\d+)\.115sou\.com/</LookFor>
            <SendTo>/show.aspx?id=$1</SendTo>
        </RewriterRule>

好了大功告成,你在IE地址栏输入http://shouji.115sou.com/,就可以看到http://www.115sou.com/index.aspx?id=shouji

的结果了

UrlRewriter.dll伪静态实现二级域名泛解析的更多相关文章

  1. .net iis 域名泛解析实战

    最近做个人网站想实现多个二级域名,一来为了好记,二来为了搜索引擎优化,搜索引擎对二级域名的收录还是比较快的.刚开始做了4,5个二级域名,每个都是在域名解析后台手动添加的,不过随着二级域名越来越多,发现 ...

  2. nginx 域名泛解析

    部分应用场景下要求服务器根据客户输入的二级域名地址自动访问不同的页面,比如一个服务器放置了不同的业务,商城.官网等多个业务,又不想一个个配置server, 网站目录结构入戏: html 网站根目录 m ...

  3. seo优化之域名泛解析优缺点分析

    原文地址:http://www.phpddt.com/web/analysis-of-domain-name.html 自己也算半个SEOER,虽然没从事过优化工作,由于自己很感兴趣,每天还是会去看很 ...

  4. 在Apache中利用ServerAlias设置虚拟主机接收多个域名和设置域名泛解析

    ServerAlias:服务器别名,在Apache中可以用于设置虚拟主机接收到个域名,也可以用于接收泛解析的域名.具体的设置方法如下: 一.用于设置虚拟主机接收多个域名 一个虚拟主机常常会接收多个域名 ...

  5. ASP.NET MVC 域名泛解析设置

    最近有个需求要做一个动态二级域名的网站,我们可以通过这样的方式去访问我们的网站 http://用户名.blog.com.而这里的用户名是根据程序的需要动态生成的.这里就会涉及到DNS服务器,要做相应的 ...

  6. asp.net下通过泛解析和伪静态实现二级域名的实现方法

    在net中微软已经为我们留下了接口,让我们为所欲为了. 首先我们可以通过一张图大概了解下.net的生命周期. 从 上图可以看出来,针对每个不同用户的请求,服务器都会创建一个新的HttpContext实 ...

  7. IIS URL重写找不到页面 (URLRewriter.dll伪静态)

    在网站上点右键 属性 进入主目录菜单 点击配置 找到.html扩展名 编辑 将 检查文件是否存在 的钩去掉! OK

  8. 借助微软提供的url重写类库URLRewriter.dll(1.0)实现程序自动二级域名,域名需要泛解析

    二级域名和系统中会员帐号自动关联,也就是系统中注册一个会员,会员自动就可以通过二级域名来访问,比如我的帐号是zhangsan,我在morecoder.com注册后,访问zhangsan.morecod ...

  9. 用phpcms切换中英文网页的方法(不用解析二级域名)、phpcms完成pc和手机端切换(同一域名)

    AA.phpcms进行双语切换方法(不用解析二级域名)作者:悦悦 博客地址:http://www.cnblogs.com/nuanai/ phpcms进行两种语言的切换,有一把部分的人都是进行的二级域 ...

随机推荐

  1. 2186: [Sdoi2008]沙拉公主的困惑 - BZOJ

    Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁国现 ...

  2. QT模态弹出对话框

    QDialog QWidget 默认show()都是非模态 如果需要模态显示, QDialog ==> setModal(true); show(); exec(); QWidget ==> ...

  3. [转载]C#中字典集合的两种遍历

    Dictionary<string, string> dictionary = new Dictionary<string,string>(); foreach (string ...

  4. [转载]C#中int和IntPtr相互转换

    方法一. int转IntPtr int i = 12;           IntPtr p = new IntPtr(i); IntPtr转int int myi = (int)p;         ...

  5. boost序列化

    #include <iostream> #include <boost/serialization/serialization.hpp> #include <boost/ ...

  6. 官方 Animator 例子解析 Animator.MatchTarget

    一.官方的解释 Animator.MatchTargetSwitch to Manual ); Parameters matchPosition The position we want the bo ...

  7. linux源代码阅读笔记 get_free_page()代码分析

    /* 34 * Get physical address of first (actually last :-) free page, and mark it 35 * used. If no fre ...

  8. 数据库批量插入数据的shell脚本

    测试用,先来一个简单的,这个是国产神通数据库的,用isql命令: !/bin/bash == "-h" ] then echo "USAGE: $0 table_name ...

  9. HDU 2473 Junk-Mail Filter(并查集+删点,设立虚父节点/找个代理)

    题意:有N封邮件, 然后又两种操作,如果是M X Y , 表示X和Y是相同的邮件.如果是S X,那么表示对X的判断是错误的,X是不属于X当前所在的那个集合,要把X分离出来,让X变成单独的一个.最后问集 ...

  10. C#画图

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...