Thursday, August 20, 2015

Helicon ISAPI Rewirte

 IIS   ISAPI Rewrite

▌背景

“因緣際會”下在同仁從IIS 6移轉ASP網站到IIS 7時,發生許多網頁無法正常載入的情況。
了解一下情況後,才發現有使用ISAPI Rewrite做URL Rewriting,藏了一些必要Url參數在Rewriting的Url裡面。

因本人對於Regular Expression風格的設定實在不是很好上手。
如同Scott Hanselman 在他文章*中提及 …

"So you've got a problem, and you want to use Regular Expressions to solve it. Now you've got two problems."

所以在這邊紀錄一下如何初步的設定ISAPI Rewriting。

▌環境

l   Windows 7 Enterprise, Windows Server 2008 R2 Enterprise
l   IIS 7.5


▌Setup


▋安裝後的目錄及檔案

預設安裝路徑C:\Program Files\Helicon\ISAPI_Rewrite3
下的幾個主要檔案:

n   Helicon Manager.exe :主要設定程式
n   httpd.conf : Rewriting 規則設定檔
n   ISAPI_Rewrite.chm :使用說明文件
n   *.dll
在IIS「新增ISAPI篩選器」時,需要直接選取對應的ISAPI DLL。
n   error.log & Rewrite.log
開啟LOG功能後,便可以使用這兩個log檔進行debug或查詢。

▋IIS設定

選取IIS最上層的主機名稱上,打開「ISAPI篩選器」

確定一下ISAPI_Rewrite是否已加入~~
PS. 並點選右方動作的【檢視已排序的清單】,確認其順序在最前面。


▋Helicon Manager

在免費版本 (Lite) 下只能針對整個IIS (如上圖的IIS Web Sites)做設定,付費版本才可以對每個網站或Web Application做單獨的設定。

點選【Edit】會直接開啟Helicorn Config Editor 編輯httpd.conf
編輯後,請在Config Editor 做【Apply】之後,在Helicon Manager必須再選擇【套用】。
如果更改的設定一直未生效,建議
1.  清除瀏覽器的cache
2.  重啟IIS。

▋How to write the Url rewriting rules?

先建立如下的測試網站,

包含
$\Test\index.html
$\Test\home.html
$\Test\Path\aaa.html
$\Test\Path\bbb.html
$\Test\Path\ccc.html
並在每個html 內容裡加上自己名稱。 例如 index.html :

<html>
<head></head>
<body>index</body>
<html>





開啟Helicon Manager,設定以下基本Rewriting rules:

RewriteEngine On

RewriteCond %{HTTP:Host} ^localhost$ [NC]
RewriteRule ^/Test$ /Test/index.html [NC]
RewriteRule ^/Test/index.html$ /Test/home.html [NC,L]

如上範例,說明如下;
1.  此規則套用範圍為 localhost 下所有網站
2.  [NC] : 忽略大小寫
3.  [L]:Last rule flag ,當符合此規則時,不再檢查及套用其他規則。
4.  規則一:將 http://localhost/Test Redirect to http://localhost/Test/index.html
5.  規則二:將http://localhost/Test/index.html Redirect to http://localhost/Test/home.html

輸入 http://localhost/Test 的結果如下。
目前已覆寫原網址(internal redirect)到http://localhost/Test/home.html
但是Client端看到的仍為原網址。


在每個Rewrite rule加上 [R] : 表示會做導向(Redirecting)的動作。
RewriteEngine On

RewriteCond %{HTTP:Host} ^localhost$ [NC,R]
RewriteRule ^/Test$ /Test/index.html [NC,R]
RewriteRule ^/Test/index.html$ /Test/home.html [NC,L,R]

結果如下,



預設Client端收到的Http Status code為302, 如果希望收到的Status code為301
可直接指定 ,例如:

RewriteRule ^/Test$ /Test/index.html [NC,R=301]


▋Advanced rewriting rules

# Debug mode
RewriteLogLevel 9
LogLevel debug

# Avoid infinite redirecting loop
RewriteRule ^/Test/(.+)/ccc.aspx.*$ - [NC,L]
# Ex. http://localhost/Test/XXX/xxx.aspx => http://localhost/Test/XXX/ccc.aspx
RewriteRule ^/Test/(.+)/(.+\.aspx)$ /Test/Path/ccc.aspx [NC,L,R]
# Ex. http://localhost/Test/XXX/xxx.aspx?flg=true => http://localhost/Test/XXX/ccc.aspx?flg=true
RewriteRule ^/Test/(.+)/(.+\.aspx)\?(.*)$ /Test/$1/ccc.aspx\?$3 [NC,L,R]


1.  Debug mode :

RewriteLogLevel
可設定 0-9 (no logging – max level)
LogLevel可直接參考官網說明。

2.  Rule 1 : Avoid infinite redirecting loop

n   一定要加上[L] , 否則會繼續判斷其他規則,造成無限導向迴圈。 (如下圖)


3.  Rule 2 : Redirect to a new url

n   注意特殊字元前面要加 ‘\’

4.  Rule 3 : Redirect to a new url with url parameters

測試如下…

 



▌Reference








No comments:

Post a Comment