第二,重写URLRewrite里面的两个方法。
1.BaseModuleRewriter.cs 里面的BaseModuleRewriter_AuthorizeRequest方法
将
[c-sharp] view plaincopy
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender
Rewrite(app.Request.Path, app)
}
[c-sharp] view plaincopy
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender
Rewrite(app.Request.Path, app)
}
改为
[c-sharp] view plaincopy
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender
Rewrite(app.Request.Url.AbsoluteUri, app)
}
[c-sharp] view plaincopy
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender
Rewrite(app.Request.Url.AbsoluteUri, app)
}
2, ModuleRewriter.cs 里面的 Rewrite 方法
将
[c-sharp] view plaincopy
protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
{
// log information to the Trace object.
app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter")
// get the configuration rules
RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules
// iterate through each rule...
for(int i = 0i <rules.Counti++)
{
// 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
}
}
// Log information to the Trace object
app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter")
}
}
[c-sharp] view plaincopy
protected override void Rewrite(string requestedPath,
System.Web.HttpApplication app)
{
// log information to the Trace object.
app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter")
// get the configuration rules
RewriterRuleCollection rules =
RewriterConfiguration.GetConfig().Rules
// iterate through each rule...
for(int i = 0i <rules.Counti++)
{
// 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
}
}
// Log information to the Trace object
app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter")
}
}
改为
[c-sharp] view plaincopy
protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
{
// log information to the Trace object.
app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter")
// get the configuration rules
RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules
// iterate through each rule...
for(int i = 0i <rules.Counti++)
{
// 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
}
}
// Log information to the Trace object
app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter")
}
}
[c-sharp] view plaincopy
protected override void Rewrite(string requestedPath,
System.Web.HttpApplication app)
{
// log information to the Trace object.
app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter")
// get the configuration rules
RewriterRuleCollection rules =
RewriterConfiguration.GetConfig().Rules
// iterate through each rule...
for(int i = 0i <rules.Counti++)
{
// 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
}
}
// Log information to the Trace object
app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter")
}
}
就是将
string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$"
改成了
string lookFor = "^" + rules[i].LookFor + "$"
完成这过程之后将整个项目重新编译一下。
这些都做完之后,
在webconfig配置里面的
<configSections>里面 添加:
[xhtml] view plaincopy
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
[xhtml] view plaincopy
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
在</configSections>下面添加
[xhtml] view plaincopy
<RewriterConfig>
<!-- 处理默认首页失败-->
<Rules>
<RewriterRule>
<LookFor>http://www/.worldbao/.com/</LookFor>
<SendTo>~/default.aspx</SendTo>
</RewriterRule>
</Rules>
<!-- 处理默认首页失败-->
<!--二级域名正则-->
<Rules>
<RewriterRule>
<LookFor>http://([a-zA-Z|0-9]+)/.worldbao/.com/</LookFor>
<SendTo>/user/user.aspx?us=$1</SendTo>
</RewriterRule>
</Rules>
<!--二级域名正则-->
</RewriterConfig>
[xhtml] view plaincopy
<RewriterConfig>
<!-- 处理默认首页失败-->
<Rules>
<RewriterRule>
<LookFor>http://www/.worldbao/.com/</LookFor>
<SendTo>~/default.aspx</SendTo>
</RewriterRule>
</Rules>
<!-- 处理默认首页失败-->
<!--二级域名正则-->
<Rules>
<RewriterRule>
<LookFor>http://([a-zA-Z|0-9]+)/.worldbao/.com/</LookFor>
<SendTo>/user/user.aspx?us=$1</SendTo>
</RewriterRule>
</Rules>
<!--二级域名正则-->
</RewriterConfig>
在httpModules里面添加
[xhtml] view plaincopy
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
</httpModules>
[xhtml] view plaincopy
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
</httpModules>
可以在域名解析中做隐藏URL转发来实现,这样做访问没问题,只是有时用户提交内容不能完成。再就是使用UrlRewrite现实了,如:ISAPI_Rewrite,使用以下规则:
RewriteCond %{HTTP:Host} ^bbs\.abc\.com$
RewriteRule /(.*) /bbs/$1 [NC,L,NS]
RewriteCond %{HTTP:Host} ^bbs\.abc\.com$
RewriteRule Scripts/(.*) /Scripts/$1 [NC,L,NS]
第一条规则是规定第二条规则的访问域的。
当然域名要做bbs.abc.com的解析或泛解析。
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)