logo 广告
Loading...
 导航 所在位置:论坛首页 -> ┈┋电脑网络┋┈ -> Asp/Asp.Net教程 -> 利用UrlRewriter 实现二级域名
回复
标题:利用UrlRewriter 实现二级域名收藏 编辑 删除 楼主 | 上一篇 下一篇
天下有雪
头像
等级:职业侠客
权限:普通用户
积分:74
金钱:224
声望:100
经验:100
发帖数:100
注册:2008年9月1日
资料 短消息2008-9-21 16:12:19

首先我们得修改UrlRewriter,怎么修改请参见江大鱼的BLog。

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

  修改完了这后,我们再把此 UrlRewriter.dll COPY 到我们项目的Bin目录下。这样就结了么?没有。

  首先请确定你的项目之前有按我上篇文章中写到的那样做过UrlRewriter的配置,否则请先回过头来看看那篇文章。

  如果你的项目已配置过,那么,我们还要为此做以下几件事情:

  1。请确定你的域名是支持泛解析的。然后你的网站为默认网站,否则将不能实现(至少我现在还没有找到好办法)

  2。在IIS配置:在IIS你的站点属性主目录配置映谢 在通配符应用程序配置处插入一个新的映谢。把可执行文件设为和上面ASPX页面同样的配置即可(注意不要勾选 “确定文件是否存在”)。(用处就是使所有请求通过 asp.net 的ISAPI来处理,只有这样才能对所有地址进行重写嘛。)

  3。查看下你的网站主机头,里面的第一个主机头值必须为空,否则会出现错误的请求。后面就随你加了,看你想绑定多少域名了。(这个办法是江大鱼想出来的。为这个错误我们都想了好多办法。在这里感谢江大鱼。。。)

  4。最后改写你的 web.config 文件。

  把上节中说到的

 <httpHandlers>
   <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
   <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
 </httpHandlers>

  改为:

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


  (就是改用HTTP 模块来执行重写,而不用HTTP 程序,否则无法重写地址前面。)

  然后就来修改我们的重写正则了:

       <RewriterRule>
          <LookFor>http://(.[0-9]*).ddvip.com/</LookFor>
          <SendTo>~/Search/Search_Sell.aspx?id={GetProperty(Content)}</SendTo>
       </RewriterRule>

  好了,现在你输入 http://1.178b2b.com/ 就能搜索出相应分类了。但是聪明的你马上就发现。晕死,首页进不去了。呵呵。当然喽。你还得为首页加入重写正则。

       <RewriterRule>
          <LookFor>http://www.ddvip.com/</LookFor>
          <SendTo>~/index.htm</SendTo>
       </RewriterRule>

  大功告成。感觉爽死了吧。呵呵。莫急,如果你二级域名指向的目录下面的页面都用的相对地址连接的图片和其它页面的话,呵呵,你有得忙了,你要全部改成如下方式:

  <a href=http://www.ddvip.com/cxlm/league.html target="_blank">豆豆网</a>

  以上就是用UrlRewriter实现二级域名的方法了。希望各位一切顺利

签名

2008-9-21 16:12:19 顶部
第1页 共页 共0个回复     <<    >>    
 快速回复
  • 支持UBB,HTML标签

  • 高级回复

  • 操作选项:评分 加精 解精 奖惩 设专题 设公告 解公告 固顶 总固顶 解固顶 结帖 解结帖 锁帖 解锁 移帖 删帖
      首页 | 购买指南 | 虚拟主机 | 特色介绍 | 下载中心 | 支付方式
    Copyright 2004-2008 BBSGood.com Powered By: BBSGood.Speed Version 5.0
      咨询电话:0575-85513832、0575-85513825(传真)、7*24小时咨询服务:13606552007 不良信息举报中心 浙ICP备05029817号
      业务QQ:38958768、客服QQ1:415896239、客服QQ2:343896043、MSN:jccsxx@hotmail.com