最最簡(jiǎn)單的實(shí)現,就是從上面的這個(gè)網(wǎng)址上下載MSDNURLRewriting.msi,安裝完成后,就生成了一個(gè)解決方案,里面有實(shí)現的原代碼.通常情況下,這些原代碼可以不用改,直接使用.那么我們共同看一下如果實(shí)現一個(gè)最簡(jiǎn)單的重寫(xiě).
1.新建立一個(gè)web項目,添加剛才下載下來(lái)生成的dll.
2.修改web.config,這比較重要,所有的重寫(xiě)規則都要在這里寫(xiě).
示例:
在<configuration></configuration>中加入:
<configSections>
<section name="RewriterConfig"
type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/(\d{4})/(\d{2})/Default\.aspx</LookFor>
<SendTo>~/Default.aspx?ID=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
然后在<system.web></system.web>中加入:
<httpHandlers>
<add verb="*" path="*.aspx"
type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
效果出來(lái)了。
上面的<LookFor>~/(\d{4})/(\d{2})/News\.aspx</LookFor>這句這正則表達式URL,即被重寫(xiě)的URL,而<SendTo>~/Default.aspx?ID=$1</SendTo>這一句為原始URL地址。其中的$1為第一個(gè)正則表達式值(上面例子為:2004),以此類(lèi)推,第二個(gè)即為$2
簡(jiǎn)單吧,這就是最簡(jiǎn)單的應用了,不過(guò)這已經(jīng)可以滿(mǎn)足了最常見(jiàn)的情況了.但是這種方法有一個(gè)弊端,那就是不能修改域名前面的部分,什么意思呢??我們經(jīng)常會(huì )看到一些網(wǎng)址是類(lèi)似于這樣的,http://use1.abc.com,這可以理解為一個(gè)用戶(hù)在這個(gè)網(wǎng)站上的網(wǎng)址,對于這種方式,上面的方法就不能實(shí)現了,這要用到兩種技術(shù),泛域名解析+URL重寫(xiě).
什么是泛域名解析,這個(gè)網(wǎng)上的解釋很多,我在這里就不多說(shuō)了.在做完泛域名解析后,就可以修改web.config里面的內容了,
只需改成下面的樣子就可以了.
<configuration>
<configSections>
<section name="RewriterConfig"
type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/([a-zA-Z0-9]*)\.aspx</LookFor>
<SendTo>~/List.aspx?Info=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/([a-zA-Z0-9]*)\.html</LookFor>
<SendTo>~/List.aspx?Info=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>http://([a-zA-Z0-9]*)\.blog\.chp365\.cn/</LookFor>
<SendTo>~/webuserblog/$1/default.htm</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
<system.web>
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>
另外,還要在IIS中,添加一個(gè)通配符應用程序映射,指到aspx的那個(gè)文件就可以了.
下面我引用一下網(wǎng)友KILLHAND的兩篇文章來(lái)說(shuō)一下如何在實(shí)踐中運用.他的文章寫(xiě)的很好.
UrlReWriter 使用經(jīng)驗小結
#UrlRewriter 是微軟封裝好了的一個(gè)URL重寫(xiě)組件。使用它可以讓我節約很多自已開(kāi)發(fā)的時(shí)間。
好了,開(kāi)始講述我的應用經(jīng)驗,這只是很菜鳥(niǎo)的經(jīng)驗,高手就不用看了。
第一步,請從此下載此組件。解壓,把UrlRewriter.dll copy到你的項目 bin 目錄下。
第二步,在Web.config中加入:
<?xml version="1.0" encoding="gb2312" ?>
<configuration>
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
第二步,加入重寫(xiě)的規則節點(diǎn):
如:
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/Sell/(.[0-9]*)\.html</LookFor>
<SendTo>~/Search/Search_Sell.aspx?id=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Sell/Search_Sell\.aspx</LookFor>
<SendTo>~/Search/Search_Sell.aspx</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Buy/(.[0-9]*)\.html</LookFor>
<SendTo>~/Search/Search_Buy.aspx?id=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Buys/(.[0-9]*)\.html</LookFor>
<SendTo>~/Buys/Show.aspx?id=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
這個(gè)就要根據你的需要了,如果你對正則表達式不熟,那么沒(méi)辦法,要么憑借你的高智商去找其中規律,稍稍改一下就能為你所用了。呵呵。如果實(shí)在搞不清,那就自己GOOGLE一下正則表達式吧。(本人開(kāi)始是參考別人的配置猜的,竟然用對了,呵呵。后來(lái)還是看了一下相關(guān)資料,發(fā)現這東東很有用。)
第三步,加入模塊配置(寫(xiě)在<system.web>里面):
如:
<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
(這里表示使用HTTP程序來(lái)處理重寫(xiě))
好了,到了現在我們可以試一下看。
第四步,在IIS\你的站點(diǎn)\屬性\主目錄\配置\映謝 加入一個(gè)和 aspx 頁(yè)面的配置相同的擴展名項。注意“確認文件是否存在”不要勾選,否則會(huì )出現找不到文件。
現在再來(lái)試試看。什么?#¥%#¥%#,還是不行。呵呵。不要急,咱們回過(guò)頭再來(lái)看看,原來(lái)在 web.config 中我們沒(méi)有配置 .html 也使用模塊此解析。
第五步,在模塊配置中加入:
<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
現在總可以了吧,呵呵。終于看到了,興奮吧。不要急,這還只是最簡(jiǎn)單的。如果你的頁(yè)面有回傳。比如說(shuō)放了DATAGRID,有分頁(yè)的,你點(diǎn)到下一頁(yè)就發(fā)現,暈倒,又出問(wèn)題了。
這下怎么辦呢,這個(gè)其實(shí)微軟件的網(wǎng)站上就有說(shuō)到,我在這里簡(jiǎn)述一下了。
第六步,加入窗體回傳保持的組件:
在原來(lái)你下載的項目里找到 ActionlessForm.dll 放到你的項目 bin 目錄下。
然后在你的這個(gè)頁(yè)面中加入:
<%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %>
再把你的<Form...>改為:
<skm:Form id="你的表單名" method="post" runat="server">
.....
</skm:Form>
That's All.現在你可以高枕無(wú)憂(yōu)了。一切如你所愿。
最后,恭祝各位一切順利。
利用UrlRewriter 實(shí)現二級域名
從上一篇文章,我們可以實(shí)現對域名后面的那部分進(jìn)行重寫(xiě),那么可不可以對前面那部分進(jìn)行重寫(xiě)而實(shí)現二級域名呢?
答案是肯定的。
這樣,首先我們得修改UrlRewriter,怎么修改請參見(jiàn)江大魚(yú)的BLog。
1.BaseModuleRewriter.cs
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender;
Rewrite(app.Request.Path, app);
}
改為
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
{
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++)
{
// 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 = "^" + 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處改動(dòng)之后重新編譯項目,將生成的dll復制到bin目錄下。
修改完了這后,我們再把此 UrlRewriter.dll COPY 到我們項目的Bin目錄下。這樣就結了么?沒(méi)有。
首先請確定你的項目之前有按我上篇文章中寫(xiě)到的那樣做過(guò)UrlRewriter的配置,否則請先回過(guò)頭來(lái)看看那篇文章。
如果你的項目已配置過(guò),那么,我們還要為此做以下幾件事情:
1。請確定你的域名是支持泛解析的。然后你的網(wǎng)站為默認網(wǎng)站,否則將不能實(shí)現(至少我現在還沒(méi)有找到好辦法)
2。在IIS配置:在IIS\你的站點(diǎn)\屬性\主目錄\配置\映謝 在通配符應用程序配置處插入一個(gè)新的映謝。把可執行文件設為和上面ASPX頁(yè)面同樣的配置即可(注意不要勾選 “確定文件是否存在”)。(用處就是使所有請求通過(guò) asp.net 的ISAPI來(lái)處理,只有這樣才能對所有地址進(jìn)行重寫(xiě)嘛。)
3。查看下你的網(wǎng)站主機頭,里面的第一個(gè)主機頭值必須為空,否則會(huì )出現錯誤的請求。后面就隨你加了,看你想綁定多少域名了。(這個(gè)辦法是江大魚(yú)想出來(lái)的。為這個(gè)錯誤我們都想了好多辦法。在這里感謝江大魚(yú)。。。)
4。最后改寫(xiě)你的 web.config 文件。
把上節中說(shuō)到的
<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
改為:
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>
(就是改用HTTP 模塊來(lái)執行重寫(xiě),而不用HTTP 程序,否則無(wú)法重寫(xiě)地址前面。)
然后就來(lái)修改我們的重寫(xiě)正則了:
<RewriterRule>
<LookFor>http://(.[0-9]*)\.178b2b\.com/</LookFor>
<SendTo>~/Search/Search_Sell.aspx?id=$1</SendTo>
</RewriterRule>
好了,現在你輸入
http://1.178b2b.com/ 就能搜索出相應分類(lèi)了。但是聰明的你馬上就發(fā)現。暈死,首頁(yè)進(jìn)不去了。呵呵。當然嘍。你還得為首頁(yè)加入重寫(xiě)正則。
<RewriterRule>
<LookFor>http://www\.178b2b\.com/</LookFor>
<SendTo>~/index.htm</SendTo>
</RewriterRule>
大功告成。感覺(jué)爽死了吧。呵呵。莫急,如果你二級域名指向的目錄下面的頁(yè)面都用的相對地址連接的圖片和其它頁(yè)面的話(huà),呵呵,你有得忙了,你要全部改成如下方式:
<a href=http://www.178b2b.com/cxlm/league.html target="_blank">誠信聯(lián)盟</a>
以上就是用UrlRewriter實(shí)現二級域名的方法了。希望各位一切順利。
最后,感謝江大魚(yú)的無(wú)私幫助。
再次對KILLHEAD和江大魚(yú)表示感謝.
說(shuō)的不是很清楚,那是因為我文筆不好,如果有朋友想共同研究一下的話(huà),可以加我QQ:120854833.或email給我:chenghp986@sohu.com