URLRewriter实现二级域名重写的问题,急(帮顶有分)
我用微软提供的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);
// 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 + "$ ";
没能实现,望各位高手不吝赐教,谢谢~
[解决办法]
string sendToUrl = RewriterUtils.ResolveUrl
在这里加入断点,看看有没有捕获到
可能是你的正则有问题
string lookFor = rules[i].LookFor;
直接试试看
[解决办法]
帮顶先
[解决办法]
听说顶也有分!!!!
[解决办法]
要实现二级URL,主要的关键步骤是两步:
第一步:泛域名的支持
第二步:URL重定向
所谓泛域名支持,打个比方:你有一个域名abc.com,在DNS上建个主机*.abc.com,指向192.168.1.1这个IP地址,使类似a.abc.com,b.abc.com,...(xxx.abc.com)的URL都指向192.168.1.1这个IP地址。----百度一上,文章很多
URL重定向:在BeginRequest(或者其他合适的时机)的时候,捕获请求的URL,然后获取URL中的关键数据,重新定位URL。
我有个很简单的例子,在我的机子上测试是没问题的,没有用到任何URL重定向的控件。使用时请设置一下DNS
在global.asax中,
void Application_BeginRequest(object sender, EventArgs e)
{
//abc.jld.com ==> jld.com/Default.aspx?ID=abc
if (System.Web.HttpContext.Current.Request.Url.ToString().ToLower() == "http://abc.microjld.com/default.aspx ")
{
System.Web.HttpContext.Current.RewritePath( "Default.aspx?ID=abc ");
}
}
[解决办法]
up
[解决办法]
我搞了很久很久,毛都没搞出来,和lz一起等。
[解决办法]
我也是在做这样一个东西,搞的投都大了,还是搞不好......
[解决办法]
JGood(他山之石,可以攻玉)
是正确答案
[解决办法]
关注中。。。