蓝派网(www.lan27.com)-精选网络资源,分享和交流! 文章首页站内搜索在线手册广告代码酷站欣赏万年历
您现在的位置: 动易网络 >> 文章中心 >> 网络编程 >> IIS配置 >> 正文

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

作者:佚名    文章来源:本站原创    更新时间:2013/5/30 17:26:20

  例:原请求网址: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文件,写个虚假的域名指向程序!


发表评论】【打印此文】【关闭窗口】【点击数:
★好玩的休闲小游戏★