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