我用微软提供的URLRewriter实现
当用户访问:http://liwx.lkg.com.cn/时,
实际上是访问:http://www.lkg.com.cn/Company/liwx/
在web.config中加入
<configSections>
<section name= "RewriterConfig " type= "URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter " />
</configSections>
<RewriterConfig>
<RewriterRule>
<LookFor> http://(\w+)\.lkg\.com\.cn\ </LookFor>
<SendTo> http://www.lkg.com.cn/Company/$1/ </SendTo>
</RewriterRule>
</RewriterConfig>
<system.web>
<compilation debug= "true "/>
<authentication mode= "Windows "/>
<httpModules>
<add type= "URLRewriter.ModuleRewriter, URLRewriter " name= "ModuleRewriter " />
</httpModules>
</system.web>
</configuration>
将微软的URLRewrite修改了两处:
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);