原文地址为:
ASP.NET中实现二级或多级域名(修改UrlRewrite)
大家应该知道,微软的 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
protected
virtual
void
BaseModuleRewriter_AuthorizeRequest(
object
sender, EventArgs e)
![24a924a57ba6b3f2b51fc9edb7ea4186.jpe](https://i-blog.csdnimg.cn/blog_migrate/156b3a452247379d066a7687077a8e2f.png)
{
HttpApplication app = (HttpApplication) sender;
Rewrite(app.Request.Path, app);
}
改为
protected
virtual
void
BaseModuleRewriter_AuthorizeRequest(
object
sender, EventArgs e)
![24a924a57ba6b3f2b51fc9edb7ea4186.jpe](https://i-blog.csdnimg.cn/blog_migrate/156b3a452247379d066a7687077a8e2f.png)
{
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
++
)
![24a924a57ba6b3f2b51fc9edb7ea4186.jpe](https://i-blog.csdnimg.cn/blog_migrate/156b3a452247379d066a7687077a8e2f.png)
{
// 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) + "$";
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
// Create a regex (note that IgnoreCase is set
)
Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
// See if a match is found
if (re.IsMatch(requestedPath))
![715f2d05503b99d41f3b6ba2cdccc84d.jpe](https://i-blog.csdnimg.cn/blog_migrate/8597f1d83cc2368df9f060992c3dbeab.png)
{
// match found - do any replacement needed
string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
// log rewriting information to the Trace object
app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
// Rewrite the URL
RewriterUtils.RewriteUrl(app.Context, sendToUrl);
break; // exit the for loop
}
}
改为
for
(
int
i
=
0
; i
<
rules.Count; i
++
)
![24a924a57ba6b3f2b51fc9edb7ea4186.jpe](https://i-blog.csdnimg.cn/blog_migrate/156b3a452247379d066a7687077a8e2f.png)
{
// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
string lookFor = "^" + rules[i].LookFor + "$";
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
// Create a regex (note that IgnoreCase is set
)
Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
// See if a match is found
if (re.IsMatch(requestedPath))
![715f2d05503b99d41f3b6ba2cdccc84d.jpe](https://i-blog.csdnimg.cn/blog_migrate/8597f1d83cc2368df9f060992c3dbeab.png)
{
// match found - do any replacement needed
string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
// log rewriting information to the Trace object
app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
// 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
>
好了大功告成,你在IE地址栏输入 http://1234.abc.com/,就可以看到 http://www.abc.com/show.aspx?id=1234
的结果了
若你在实际应用中碰到了问题,请查看文章 "修改UrlRewrite以对域名进行重写"需要注意的问题 ,希望能够帮助你!
附:
URLRewriter 的相关资料
http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx
转载请注明本文地址: ASP.NET中实现二级或多级域名(修改UrlRewrite)
大家应该知道,微软的 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
![](https://i-blog.csdnimg.cn/blog_migrate/30d0d8ffd4dbf85ca6de25ae391dcc39.png)
![24a924a57ba6b3f2b51fc9edb7ea4186.jpe](https://i-blog.csdnimg.cn/blog_migrate/156b3a452247379d066a7687077a8e2f.png)
![9310e85a14af99de4811ff4c77f1f911.jpe](https://i-blog.csdnimg.cn/blog_migrate/a9af314cccc3b1c20a00c16e7c3fb329.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a64ee9dafc08211855da6602b1505772.png)
改为
![](https://i-blog.csdnimg.cn/blog_migrate/30d0d8ffd4dbf85ca6de25ae391dcc39.png)
![24a924a57ba6b3f2b51fc9edb7ea4186.jpe](https://i-blog.csdnimg.cn/blog_migrate/156b3a452247379d066a7687077a8e2f.png)
![9310e85a14af99de4811ff4c77f1f911.jpe](https://i-blog.csdnimg.cn/blog_migrate/a9af314cccc3b1c20a00c16e7c3fb329.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a64ee9dafc08211855da6602b1505772.png)
就是将 app.Request.Path 替换成了 app.Request.Url.AbsoluteUri
2.ModuleRewriter.cs
![](https://i-blog.csdnimg.cn/blog_migrate/30d0d8ffd4dbf85ca6de25ae391dcc39.png)
![24a924a57ba6b3f2b51fc9edb7ea4186.jpe](https://i-blog.csdnimg.cn/blog_migrate/156b3a452247379d066a7687077a8e2f.png)
![9310e85a14af99de4811ff4c77f1f911.jpe](https://i-blog.csdnimg.cn/blog_migrate/a9af314cccc3b1c20a00c16e7c3fb329.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![715f2d05503b99d41f3b6ba2cdccc84d.jpe](https://i-blog.csdnimg.cn/blog_migrate/8597f1d83cc2368df9f060992c3dbeab.png)
![e083dfde5a91f50979fe8979b4012b9d.jpe](https://i-blog.csdnimg.cn/blog_migrate/75b4174cddbc552a6e3102725c6f16ea.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cb5c401b33a814d724e38e50b0a8f214.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a64ee9dafc08211855da6602b1505772.png)
改为
![](https://i-blog.csdnimg.cn/blog_migrate/30d0d8ffd4dbf85ca6de25ae391dcc39.png)
![24a924a57ba6b3f2b51fc9edb7ea4186.jpe](https://i-blog.csdnimg.cn/blog_migrate/156b3a452247379d066a7687077a8e2f.png)
![9310e85a14af99de4811ff4c77f1f911.jpe](https://i-blog.csdnimg.cn/blog_migrate/a9af314cccc3b1c20a00c16e7c3fb329.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![715f2d05503b99d41f3b6ba2cdccc84d.jpe](https://i-blog.csdnimg.cn/blog_migrate/8597f1d83cc2368df9f060992c3dbeab.png)
![e083dfde5a91f50979fe8979b4012b9d.jpe](https://i-blog.csdnimg.cn/blog_migrate/75b4174cddbc552a6e3102725c6f16ea.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1553bc1c863899173c87ebf2aa836c76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cb5c401b33a814d724e38e50b0a8f214.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a64ee9dafc08211855da6602b1505772.png)
将
string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
改成了
string lookFor = "^" + rules[i].LookFor + "$";
完成这2处改动之后重新编译项目,将生成的dll复制到bin目录下。
再就是写web.config里的重写正则了
![](https://i-blog.csdnimg.cn/blog_migrate/30d0d8ffd4dbf85ca6de25ae391dcc39.png)
![](https://i-blog.csdnimg.cn/blog_migrate/30d0d8ffd4dbf85ca6de25ae391dcc39.png)
![](https://i-blog.csdnimg.cn/blog_migrate/30d0d8ffd4dbf85ca6de25ae391dcc39.png)
![](https://i-blog.csdnimg.cn/blog_migrate/30d0d8ffd4dbf85ca6de25ae391dcc39.png)
好了大功告成,你在IE地址栏输入 http://1234.abc.com/,就可以看到 http://www.abc.com/show.aspx?id=1234
的结果了
若你在实际应用中碰到了问题,请查看文章 "修改UrlRewrite以对域名进行重写"需要注意的问题 ,希望能够帮助你!
附:
URLRewriter 的相关资料
http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx
作者:
江大鱼
出处: http://jzywh.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处: http://jzywh.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
转载请注明本文地址: ASP.NET中实现二级或多级域名(修改UrlRewrite)