ASP.NET中实现二级或多级域名(修改UrlRewrite)
来源:互联网 发布:国际数据公司 idc 编辑:程序博客网 时间:2024/06/07 08:29
本文转自:http://www.cnblogs.com/jzywh/archive/2005/09/29/246650.Html
大家应该知道,微软的URLRewrite能够对URL进行重写,但是也只能对域名之后的部分进行重写,而不能对域名进行重写,如:可将 http://http://www.abc.com//1234/ 重写为 http://www.abc.com/show.aspx?id=1234 但不能将
http://1234.abc.com/ 重写为 http://www.abc.com/show.aspx?id=1234。
要实现这个功能,前提条件就是 http://www.abc.com/ 是泛解析的,再就是要修改一下URLRewriter了。
总共要修改2个文件
1.BaseModuleRewriter.cs
改为
就是将 app.Request.Path 替换成了 app.Request.Url.AbsoluteUri
<SCRIPT type=text/javascript><!--google_ad_client = "pub-4654382453554667";/* 468x60, 创建于 08-4-20 */google_ad_slot = "9836485125";google_ad_width = 468;google_ad_height = 60;//--></SCRIPT><SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript></SCRIPT>
2.ModuleRewriter.cs
改为
将
string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
改成了
string lookFor = "^" + rules[i].LookFor + "$";
完成这2处改动之后重新编译项目,将生成的dll复制到bin目录下。
再就是写web.config里的重写正则了
好了大功告成,你在IE地址栏输入http://1234.abc.com/,就可以看到http://www.abc.com/show.aspx?id=1234
的结果了
http://1234.abc.com/ 重写为 http://www.abc.com/show.aspx?id=1234。
要实现这个功能,前提条件就是 http://www.abc.com/ 是泛解析的,再就是要修改一下URLRewriter了。
总共要修改2个文件
1.BaseModuleRewriter.cs
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender;
Rewrite(app.Request.Path, app);
}
{
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);
}
{
HttpApplication app = (HttpApplication) sender;
Rewrite(app.Request.Url.AbsoluteUri, app);
}
就是将 app.Request.Path 替换成了 app.Request.Url.AbsoluteUri
<SCRIPT type=text/javascript><!--google_ad_client = "pub-4654382453554667";/* 468x60, 创建于 08-4-20 */google_ad_slot = "9836485125";google_ad_width = 468;google_ad_height = 60;//--></SCRIPT><SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript></SCRIPT>
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
}
}
{
// 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
}
}
{
// 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目录下。
再就是写web.config里的重写正则了
<RewriterRule>
<LookFor>http://(/d+)/.abc/.com/</LookFor>
<SendTo>/show.aspx?id=$1</SendTo>
</RewriterRule>
<LookFor>http://(/d+)/.abc/.com/</LookFor>
<SendTo>/show.aspx?id=$1</SendTo>
</RewriterRule>
好了大功告成,你在IE地址栏输入http://1234.abc.com/,就可以看到http://www.abc.com/show.aspx?id=1234
的结果了
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)需要注意的问题
- [转]ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)需要注意的问题
- ASP.NET中实现二级或多级域名(修改UrlRewrite)需要注意的问题
- Asp.Net中实现二级或多级域名(URL重写)
- ASP.NET中实现二级或多级域名
- UrlRewrite实现二级或多级域名
- 修改UrlRewrite以对域名进行重写,即实现二级或多级域名
- 修改UrlRewrite以对域名进行重写,即实现二级或多级域名
- 修改UrlRewrite以对域名进行重写,即实现二级或多级域名
- Notepad++ 与正则表达式
- 几种常见的排序算法的分析研究
- ThoughtWorks University取经记
- JSP/Servlet:控制器(Servlet)
- 信号量和PV操作
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- mbuf
- 非安装版的PostgreSQL8.3.3的首次使用经历
- C运算符优先级口诀
- ASP.NET中实现二级或多级域名(修改UrlRewrite)需要注意的问题
- 完整的扑克牌
- 单元测试框架rgtest
- 闲扯原码、反码、补码
- SPRING分发器(DispatcherServlet)