<iframe marginwidth="0" marginheight="0" src="http://218.16.120.35:65001/PC/Global/images/b.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>
大家应该知道,微软的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,EventArgse)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
HttpApplicationapp=(HttpApplication)sender;
Rewrite(app.Request.Path,app);
}
改为
protected
virtual
void
BaseModuleRewriter_AuthorizeRequest(
object
sender,EventArgse)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
HttpApplicationapp=(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
++
)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//getthepatterntolookfor,andResolvetheUrl(convert~intotheappropriatedirectory)
stringlookFor="^"+RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath,rules[i].LookFor)+"$";
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Createaregex(notethatIgnoreCaseisset
)
Regexre=newRegex(lookFor,RegexOptions.IgnoreCase);
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Seeifamatchisfound
if(re.IsMatch(requestedPath))
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//matchfound-doanyreplacementneeded
stringsendToUrl=RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath,re.Replace(requestedPath,rules[i].SendTo));
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//logrewritinginformationtotheTraceobject
app.Context.Trace.Write("ModuleRewriter","RewritingURLto"+sendToUrl);
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//RewritetheURL
RewriterUtils.RewriteUrl(app.Context,sendToUrl);
break;//exittheforloop
}
}
改为
for
(
int
i
=
0
;i
<
rules.Count;i
++
)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//getthepatterntolookfor,andResolvetheUrl(convert~intotheappropriatedirectory)
stringlookFor="^"+rules[i].LookFor+"$";
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Createaregex(notethatIgnoreCaseisset
)
Regexre=newRegex(lookFor,RegexOptions.IgnoreCase);
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Seeifamatchisfound
if(re.IsMatch(requestedPath))
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//matchfound-doanyreplacementneeded
stringsendToUrl=RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath,re.Replace(requestedPath,rules[i].SendTo));
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//logrewritinginformationtotheTraceobject
app.Context.Trace.Write("ModuleRewriter","RewritingURLto"+sendToUrl);
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//RewritetheURL
RewriterUtils.RewriteUrl(app.Context,sendToUrl);
break;//exittheforloop
}
}
将
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能够对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
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://jzywh.cnblogs.com/Images/dot.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
改为
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://jzywh.cnblogs.com/Images/dot.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
就是将 app.Request.Path 替换成了 app.Request.Url.AbsoluteUri
2.ModuleRewriter.cs
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://jzywh.cnblogs.com/Images/dot.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/dot.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://jzywh.cnblogs.com/Images/dot.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
改为
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://jzywh.cnblogs.com/Images/dot.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/dot.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://jzywh.cnblogs.com/Images/dot.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
将
string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
改成了
string lookFor = "^" +rules[i].LookFor + "$";
完成这2处改动之后重新编译项目,将生成的dll复制到bin目录下。
再就是写web.config里的重写正则了
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://jzywh.cnblogs.com/Images/OutliningIndicators/None.gif)
好了大功告成,你在IE地址栏输入 http://1234.abc.com/,就可以看到 http://www.abc.com/show.aspx?id=1234
的结果了