新聞中心
這里要求對(duì)域名進(jìn)行重寫(xiě),實(shí)現(xiàn)http://1234.abc.com/ 到 ~/Defa.aspx?id=1234的重寫(xiě)。

***:域名
首先域名要支持泛解悉,就是域名解悉的主機(jī)名為星號(hào)*,例:*.abc.com。如下圖
這樣能保證你在瀏覽器地址欄輸入任何前綴,DNS都會(huì)把它們指向到你指定的IP地址上。
第二:IIS設(shè)置(Win2003 + IIS 6為例)
(1)網(wǎng)站必須為Web服務(wù)器的默認(rèn)站點(diǎn),即端口號(hào)為80,主機(jī)頭為空的站點(diǎn)。如下圖所示。
該站點(diǎn)接收所有對(duì)該服務(wù)器的HTTP請(qǐng)求(其它設(shè)置為主機(jī)頭的站點(diǎn)除外)。所以任何二級(jí)域名訪問(wèn)該服務(wù)器都會(huì)由該站點(diǎn)進(jìn)行處理。
(2)另外要在站點(diǎn)的“通配符應(yīng)用程序映射”列表中添加ASP.NET的Web請(qǐng)求處理程序aspnet_isapi.dll。如下圖所示。
在這里的設(shè)置,是讓該站點(diǎn)接到的所有請(qǐng)求都交給aspnet_isapi.dll處理。
第三:修改Microsoft的URLRewriter。
運(yùn)行開(kāi)源項(xiàng)目URLRewriter。這里需要修改兩個(gè)地方:
(1)BaseModuleRewriter.cs類(lèi)
- protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
- {
- HttpApplication app = (HttpApplication) sender;
- //Rewrite(app.Request.Path, app);
- Rewrite(app.Request.Url.AbsoluteUri, app); // ## ## ## 這里修改了
- }
這里將app.Request.Path 替換成了 app.Request.Url.AbsoluteUri
(2)ModuleRewriter.cs類(lèi)
- protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
- {
- // log information to the Trace object.
- app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter");
- // get the configuration rules
- RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
- // iterate through each rule...
- 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
- }
- }
- // Log information to the Trace object
- app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter");
- }
這里將string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
改成了 string lookFor = "^" + rules[i].LookFor + "$";
這兩個(gè)地方修改完以后,生成項(xiàng)目。將項(xiàng)止目bin/Debug目錄下的URLRewriter.dll文件Copy到我們要重寫(xiě)URL的項(xiàng)目中。
第四:配置項(xiàng)目
(1)在web.config文件的configSections節(jié)點(diǎn)下添加如下一行代碼
這里配置一個(gè)重寫(xiě)配置的類(lèi)
(2)修改httpModules節(jié)點(diǎn),在里面添加一行配置代碼
(3)在主節(jié)點(diǎn)configuration節(jié)點(diǎn)下添加路由規(guī)則,代碼如下:
http://(\w+).abc.com/ ~/Defa.aspx?id=$1
代碼里一個(gè)RewriterRule節(jié)點(diǎn)就是一個(gè)規(guī)則,這時(shí)只有一個(gè),即把域名中的主機(jī)頭部分做為Defa.aspx頁(yè)面的id參數(shù)的值發(fā)送給Defa.aspx頁(yè)面。
注意:這里L(fēng)ookFor節(jié)點(diǎn)里的http://(\w+).abc.com/不能少了***的反斜框
OK,一切完工
發(fā)布,上傳到服務(wù)器,測(cè)試一下,如圖
本次測(cè)試需要一個(gè)類(lèi)庫(kù):URLRewriter.dll (測(cè)試版本 1.0.1495.18710)
官網(wǎng)下載地址:http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi
當(dāng)前題目:ASP.NET下用URLRewriter重寫(xiě)二級(jí)域名
文章出自:http://m.fisionsoft.com.cn/article/dpscegg.html


咨詢(xún)
建站咨詢(xún)
