利用URLRewriter重寫實現偽二級域名
最近公司有個項目,要求將請求的參數放到放置到網址的前面
例:原請求網址:www.abc.com?jc=dfs 改寫層 dfs.abc.com,實現這種偽的二級域名。着實下了一番功夫,園里有不少大大已經寫過同樣的文章了。今天我就在這總結下。
第一步:下載一個URLRewriter。
微軟官方 關於URLRewriter的解釋
http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx
進入以后可以下載源代碼,當然也可以去別的地方下載別人修改過的代碼。要安裝,安裝完畢以后,在“我的文檔”→“MSDN”→“URL Rewriting in ASP.NET” →“URLRewritingCode.sln”
第二步:要對URLRewriter里的方法重寫
這里我們要重寫的,就是URLRewriter程序集下的兩個文件 “BaseModuleRewriter.cs”和“ModuleRewriter.cs”
1.BaseModuleRewriter.cs

/// <summary> /// Called when the module's AuthorizeRequest event fires. /// </summary> /// <remarks>This event handler calls the <see cref="Rewrite"/> method, passing in the /// <b>RawUrl</b> and HttpApplication passed in via the <b>sender</b> parameter.</remarks> protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e) { HttpApplication app = (HttpApplication) sender; Rewrite(app.Request.Path, app); }
改為

/// <summary> /// Called when the module's AuthorizeRequest event fires. /// </summary> /// <remarks>This event handler calls the <see cref="Rewrite"/> method, passing in the /// <b>RawUrl</b> and HttpApplication passed in via the <b>sender</b> parameter.</remarks> protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e) { HttpApplication app = (HttpApplication) sender; //Rewrite(app.Request.Path, app); Rewrite(app.Request.Url.AbsoluteUri, app); }
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 } }
改為

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) + "$"; 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 } }
修改完成以后,重新生成,在bin文件下找到Debug文件夾中找到URLRewriter.dll這個文件。
在自己的項目中引用這個DLL文件
第三步:在自己的項目的web.config文件中坐下配置,就可以了
1.在<configSections>節點下添加
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
2.在<configuration>節點(即根目錄的節點下)
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>http://(\w+).abc.com/</LookFor>
<SendTo>/Index.aspx?jv=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
如果有多個匹配地址可以寫多個,<Rules>節點中多寫幾個<RewriterRule> 就可以了!
3.在<system.web>節點下<httpModules>節點中添加
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
這樣,基本就配置完畢了!
第四部:如果要自己測試,要把程序放到IIS服務器上,進行測試。要自己修改下hosts文件,寫個虛假的域名指向程序!
如果有需要Demo的,請留言!園子不讓上傳文件!抱歉
雖然這樣很方便,但是就在我寫文章的同時,看到一篇關於URL重寫的園子里的一篇大大寫的文章
鏈接帖出來
http://www.cnblogs.com/csky/archive/2006/08/09/urlrewrite.html
第二次寫博客文章,雖然經常來園子,但是經常是伸手黨,現在自己寫文章,才知道不好寫啊!
歡迎大家多多和我交流!