2009年4月25日 星期六

url rewrite 元件

常常有些人會問如何隱藏 Querystring,原因是 Querystring 裡面包含一些參數不想被使用者知道。以下文章就介紹如何使用 UrlRewriter.NET 套件,來幫助我們實現 Url Rewriting 將 Querystring 隱藏起來。

首先至 <a href="http://urlrewriter.net/">Open Source URL Rewriter for .NET / IIS / ASP.NET </a>下載 UrlRewriter.NET 套件
下載 urlrewiternet20rc1b6.zip 後,將 UrlRewriterV2\bin\Release\Intelligencia.UrlRewriter.dll 加入參考
設定 Web.config 檔案
</configuration>

<configSections>
<section name="rewriter"
requirePermission="false"
type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter"/>
</configSections>

<system.web>
<httpModules>
<add name="UrlRewriter"
type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
</httpModules>
</system.web>

<rewriter>
<rewrite url="~/Books/(\d+)/Default.aspx" to="~/Books.aspx?bookID=$1"/>
</rewriter>

</configuration>
其中比較注意的是 <rewriter> 區塊的部分,url 表示來源網址,to 表示目的網址。舉例來說,你的網址是 Books/1234/Default.aspx 時,UrlRewiter 就幫你 Url 轉向 Books.aspx?bookID=1234。其中 (\d+) 是一種 Regular Expression(Regex)的表示方法,表示由 0-9 組合而成的字串組合,$1 則代表 url 符合 Regex 的字串變數。當你有多組 Regex,則就會有 $1、$2、$3...以此類推。

沒有留言:

張貼留言