http://1234.abc.com 重寫爲 http://www.abc.com/show.aspx?id=1234。
要實現這個功能,前提條件就是 www.abc.com 是泛解析的,再就是要修改一下URLRewriter了。
總共要修改2個文件
1.BaseModuleRewriter.cs
{
HttpApplication app = (HttpApplication) sender;
Rewrite(app.Request.Path, app);
}
改爲
{
HttpApplication app = (HttpApplication) sender;
Rewrite(app.Request.Url.AbsoluteUri, app);
}
就是將 app.Request.Path 替換成了 app.Request.Url.AbsoluteUri
2.ModuleRewriter.cs
{
// 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 = "^" + 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裏的重寫正則了
<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 <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>