轉(zhuǎn)載請(qǐng)注明來源:楓子整理并發(fā)布于:http://www.imfeng.com/iis-rewrite-rules-collect-isapi-rewrite/
二級(jí)域名:
RewriteCond Host: (?!/.|www|ww)(.*).gongluela.com
RewriteRule (.*) http/://www.gongluela.com/$1$2 [I,R]
防盜鏈:
ISAPI_Rewrite的httpd.ini內(nèi)容寫的是下面內(nèi)容,如果你也遇到盜鏈的困擾,建議可以參考一下:
RewriteCond Host: (.+)
RewriteCond Referer: (?!http://(?:www\.gongluela\.com|www\.imfeng\.com|www\.goboyo\.com|www\.soso\.com|www\.google\.com|cache\.baidu\.com)).+
RewriteRule .*\.(?:jpg|kmz|kml|zip) /block.gif [I,O]
注釋:第二行里面的域名是允許鏈接圖片的網(wǎng)站列表,目前我允許我自己的兩個(gè)站和另外bloglines、zhuaxia、google、baidu這四個(gè)網(wǎng)站引用我站文件,其他網(wǎng)站的引用被禁止。
第三行是禁止引用的文件類型,包括jpg圖片、Google Earth的kmz和kml、zip文件。當(dāng)然,還可以增加其他類型,比如gif、rar等,因?yàn)槲艺緵]有那些文件,所以沒有添加上去。
下面是轉(zhuǎn)載的一點(diǎn)文章,特轉(zhuǎn)給帥哥看的。
發(fā)這個(gè)幫助文件是因?yàn)樵诮o朋友設(shè)置主機(jī)的時(shí)候發(fā)現(xiàn)他們的ISAPI Rewrite的設(shè)置都有些不正確,有些甚至阻礙了站點(diǎn)的正常運(yùn)行。就把官方自帶的英文幫助粗略的翻譯了一下。大家可以自由使用和轉(zhuǎn)載。但轉(zhuǎn)載時(shí)如果你愿意請(qǐng)注明是 藍(lán)色 翻譯的版本。
===================
ISAPI Rewrite 配置簡(jiǎn)介:
在NT/2000/XP和2003平臺(tái)上,ISAPI Rewrite在系統(tǒng)帳戶下射入INETINFO進(jìn)程與 IIS 以共存模式過濾器運(yùn)行。所以系統(tǒng)帳戶應(yīng)該給予對(duì)ISAPI- REWIRITE DLLS Internet匿名訪問組 可讀可執(zhí)行權(quán)限 和所有的 HTTPD。INI 文件可讀權(quán)限,還應(yīng)該給予系統(tǒng)帳戶對(duì)于所有包括 HTTPD。INI 文件的文件夾的可寫權(quán)限,這將允許產(chǎn)生 HTTP。 PARSE。ERRORS 日志文件,來記錄配置文件語(yǔ)法錯(cuò)誤。對(duì)于PROXY模塊也需要額外的權(quán)限,因?yàn)樗鼘⑦\(yùn)行于連接池或HIGH-ISPLATED 應(yīng)用模式,IIS帳戶共享池和HIGH-ISOLATION池應(yīng)被給予對(duì)RWHELPERE。DLL的可讀權(quán)限。缺省情況下IWAM-《計(jì)算機(jī)名》被用于所有的池,在相應(yīng)的COM+應(yīng)用設(shè)置中應(yīng)借助COM+ADMINISTRATION MMC SNAP-IN建立池帳戶
配置文件格式化:
配置文件分為兩種: GLOBAL(服務(wù)器全局模式)和 INDIVIDUAL(站點(diǎn)獨(dú)立設(shè)置模式)文件,GLOBAL(服務(wù)器端全局模式)配置文件應(yīng)該被放在ISAPI-REWRITE 安裝目錄中,名為 httpd.ini 。這個(gè)文件可以通過開始菜單的快捷方式訪問和配置,這個(gè)文件里面的映射規(guī)則為全局規(guī)則,將針對(duì)所有站點(diǎn)起效。INDIVIDUAL(站點(diǎn)獨(dú)立設(shè)置模式)配置文件應(yīng)該被放在虛擬站點(diǎn)的根目錄內(nèi),也被命名為httpd.ini文件,這里面應(yīng)該放置針對(duì)站點(diǎn)的映射配置設(shè)置,只針對(duì)被放置的虛擬站點(diǎn)有效。兩種類型的 httpd.ini 都是標(biāo)準(zhǔn)的windows ini 文件。所有的映射規(guī)則應(yīng)該被放置在 [ISAPI_Rewrite] 之后。之前的文件文本將被忽略。
HTTPD.INI文件示例
[ISAPI_Rewrite]
# This is a comment
# 300 = 5 minutes
CacheClockRate 300
RepeatLimit 20
# 設(shè)置其他人沒有下載 httpd.ini 和 httpd.parse.errors 文件的權(quán)限
RewriteRule /httpd(?:\.ini|\.parse\.errors) / [F,I,O]
# Block external access to the Helper ISAPI Extension
RewriteRule .*\.isrwhlp / [F,I,O]
# 配置規(guī)則
RewriteCond Host: (.+)
RewriteCond 指令
Syntax:(句法) RewriteCond TestVerb CondPattern [Flags]
這一指令定義一個(gè)條件規(guī)則,在 RewriteRule 或者 RewriteHeader或 RewriteProxy指令前預(yù)行RewriteCond指令,后面的規(guī)則只有它的,模式匹配URI的當(dāng)前狀態(tài)并且額外的條件也被應(yīng)用才會(huì)被應(yīng)用。
TestVerb
Specifies verb that will be matched against regular expression.
特別定義的動(dòng)詞匹配規(guī)定的表達(dá)式
TestVerb=(URL | METHOD | VERSION | HTTPHeaderName: | %ServerVariable) where:
URL – returns Request-URI of client request as described in RFC 2068 (HTTP 1.1);
返回客戶端在RFC2068中描述的需求的Request-URI
METHOD – returns HTTP method of client request (OPTIONS, GET, HEAD, POST, PUT, Delete or TRACE);
返回客戶端需求(OPTIONS, GET, HEAD, POST, PUT, Delete or TRACE)的HTTP方法
VERSION – returns HTTP version;
返回HTTP版本
HTTPHeaderName – returns value of the specified HTTP header. HTTPHeaderName can be any valid HTTP header name. Header names should include the trailing colon “:”. If specified header does not exists in a client’s request TestVerb is treated as empty string.
返回特定義的HTTP頭文件的值
HTTPHeaderName =
Accept:
Accept-Charset:
Accept-Encoding:
Accept-Language:
Authorization:
Cookie:
From:
Host:
If-Modified-Since:
If-Match:
If-None-Match:
If-Range:
If-Unmodified-Since:
Max-Forwards:
Proxy-Authorization:
Range:
Referer:
User-Agent:
Any-Custom-Header
得到更多的關(guān)于HTTP頭文件的和他們的值的信息參考RFC2068
ServerVariable 返回特定義的服務(wù)器變量的值 。例如服務(wù)器端口,全部服務(wù)器變量列表應(yīng)在IIS文檔中建立,變量名應(yīng)用%符預(yù)定;
CondPattern
The regular expression to match TestVerb
規(guī)則表達(dá)式匹配TestVerb
[Flags]
Flags is a comma-separated list of the following flags:
O (nOrmalize)
Normalizes string before processing. Normalization includes removing of an URL-encoding, illegal characters, etc. This flag is useful with URLs and URL-encoded headers
RewriteRule 指令
Syntax: RewriteRule Pattern FormatString [Flags]
這個(gè)指令可以不止發(fā)生一次,每個(gè)指令定義一個(gè)單獨(dú)的重寫規(guī)則,這些規(guī)則的定義命令很重要,因?yàn)檫@個(gè)命令在應(yīng)用運(yùn)行時(shí)規(guī)則是有用途的
I (ignore case)
不管大小寫強(qiáng)行指定字符匹配(http://www.chnjc.com/),這個(gè)FLAG影響RewriteRule指令和相應(yīng)的RewriteCond 指令
F (Forbidden)
對(duì)客戶端做反應(yīng),停止REWRITING進(jìn)程并且發(fā)送403錯(cuò)誤,注意在這種情況下FORMATSTRING 是無用的并可以設(shè)置為任何非空字符串。
L (last rule)
不應(yīng)用任何重寫規(guī)則在此停止重寫進(jìn)程,使用這個(gè)FLAG以阻止當(dāng)前被重寫的URI被后面的規(guī)則再次重寫
N (Next iteration)
強(qiáng)制REWRITINGENGINE調(diào)整規(guī)則目標(biāo)并且從頭重啟規(guī)則檢查(所有修改將保存),重啟次數(shù)由RepeatLimit指定的值限制,如果這個(gè)數(shù)值超過N FLAG將被忽略
NS (Next iteration of the same rule)
以N標(biāo)記工作不從相同的規(guī)則重啟規(guī)則規(guī)則進(jìn)程(例如強(qiáng)制重復(fù)規(guī)則應(yīng)用),通過RepeatLimit指令指定一個(gè)反復(fù)實(shí)行某一規(guī)則的最大數(shù)目,
P (force proxy)
強(qiáng)制目的URI在內(nèi)部強(qiáng)制為代理需求并且立即通過ISAPI擴(kuò)展應(yīng)付代理需求,必須確認(rèn)代理字符串是一個(gè)有效的URI包括協(xié)議主機(jī)等等否則代理將返回錯(cuò)誤
R (explicit redirect)
強(qiáng)制服務(wù)器對(duì)客戶端發(fā)出重定向指示即時(shí)應(yīng)答,提供目的URI的新地址,重定向規(guī)則經(jīng)常是最后規(guī)則
RP (permanent redirect)
幾乎和[R]標(biāo)記相同但是發(fā)布301HTTP狀態(tài)而不是302HTTP狀態(tài)代碼
U (Unmangle Log)
當(dāng)URI是源需求而不是重寫需求時(shí)記載URI
O (nOrmalize)
在實(shí)行之前標(biāo)準(zhǔn)化字符串。標(biāo)準(zhǔn)化包括URL-ENCODING,不合法的字符的再移動(dòng)等,這個(gè)標(biāo)記對(duì)于URLS和URLS-ENDODED頭是有用的
CL (Case Lower)
小寫
CU (Case Upper)
大寫
RewriteHeader directive
Syntax: RewriteHeader HeaderName Pattern FormatString [Flags]
這個(gè)指令是RewriteRule的更概括化變種,它不僅重寫URL的客戶端需求部分,而且重寫HTTP頭,這個(gè)指令不僅用于重寫。生成,刪除任何HTTP頭,甚至改變客戶端請(qǐng)求的方法
HeaderName
指定將被重寫的客戶頭(http://www.yaopinwang.org/),可取的值與 RewriteCond 指令中TestVerb參數(shù)相同
Pattern
限定規(guī)則表達(dá)式以匹配Request-URI,
FormatString
限定將生成新的URI的FormatString
[Flags]
是一個(gè)下列FLAGS的命令分隔列表
I (ignore case)
不管大小寫強(qiáng)行指定字符匹配,這個(gè)FLAG影響RewriteRule指令和相應(yīng)的RewriteCond 指令
F (Forbidden)
對(duì)客戶端做反應(yīng),停止REWRITING進(jìn)程并且發(fā)送403錯(cuò)誤,注意在這種情況下FORMATSTRING 是無用的并可以設(shè)置為任何非空字符串。
L (last rule)
不應(yīng)用任何重寫規(guī)則在此停止重寫進(jìn)程,使用這個(gè)FLAG以阻止當(dāng)前被重寫的URI被后面的規(guī)則再次重寫
N (Next iteration)
強(qiáng)制REWRITINGENGINE調(diào)整規(guī)則目標(biāo)并且從頭重啟規(guī)則檢查(所有修改將保存),重啟次數(shù)由RepeatLimit指定的值限制,如果這個(gè)數(shù)值超過N FLAG將被忽略
NS (Next iteration of the same rule)
以N標(biāo)記工作不從相同的規(guī)則重啟規(guī)則規(guī)則進(jìn)程(例如強(qiáng)制重復(fù)規(guī)則應(yīng)用),通過RepeatLimit指令指定一個(gè)反復(fù)實(shí)行某一規(guī)則的最大數(shù)目,
R (explicit redirect)
強(qiáng)制服務(wù)器對(duì)客戶端發(fā)出重定向指示即時(shí)應(yīng)答,提供目的URI的新地址,重定向規(guī)則經(jīng)常是最后規(guī)則
RP (permanent redirect)
幾乎和[R]標(biāo)記相同但是發(fā)布301HTTP狀態(tài)而不是302HTTP狀態(tài)代碼
U (Unmangle Log)
當(dāng)URI是源需求而不是重寫需求時(shí)記載URI
O (nOrmalize)
在實(shí)行之前標(biāo)準(zhǔn)化字符串。標(biāo)準(zhǔn)化包括URL-ENCODING,不合法的字符的再移動(dòng)等,這個(gè)標(biāo)記對(duì)于URLS和URLS-ENDODED頭是有用的
CL (Case Lower)
小寫
CU (Case Upper)
大寫
要重移動(dòng)頭,F(xiàn)ORMAT STRING模式應(yīng)該生成一個(gè)空字符串,例如這一規(guī)則將從客戶請(qǐng)求中重移代理信息
RewriteHeader User-Agent: .* $0
并且這一規(guī)則將把OLD-URL HEADER 加入請(qǐng)求中。
RewriteCond URL (.*)RewriteHeader Old-URL: ^$ $1
最后一個(gè)例子將通過改變請(qǐng)求方法定向所有的WEBDAV請(qǐng)求到/WEBDAV。ASP
RewriteCond METHOD OPTIONS
RewriteRule (.*) /webdav.asp?$1
RewriteHeader METHOD OPTIONS GET
RewriteProxy directive
Syntax: RewriteProxy Pattern FormatString [Flags]
強(qiáng)制目的URI在內(nèi)部強(qiáng)制為代理需求并且立即通過ISAPI擴(kuò)展應(yīng)付代理需求,這將允許IIS作為代理服務(wù)器并且重路由到其他站點(diǎn)和服務(wù)器
Pattern
限定規(guī)則表達(dá)式以匹配Request-URI,
FormatString
限定將生成新的URI的FormatString
[Flags]
是一個(gè)下列FLAGS的命令分隔列表
D (Delegate security)
代理模式將試圖以當(dāng)前假冒的用戶資格登陸遠(yuǎn)程服務(wù)器,
C (use Credentials)
代理模式將試圖一在URL或基本授權(quán)頭文件中指定的資格登陸遠(yuǎn)程服務(wù)器,用這個(gè)標(biāo)記你可以使用http://user:password@host.com/path/ syntax 作為URL
F (Follow redirects)
缺省情況下ISAPI_Rewrite 將試圖將MAP遠(yuǎn)程服務(wù)器返回的重定向指令到本地服務(wù)器命名空間,如果遠(yuǎn)程服務(wù)器返回重定向點(diǎn)到那臺(tái)服務(wù)器其他的某個(gè)位置,ISAPI_Rewrite 將修改這一重定向指令指向本服務(wù)器名,這將避免用戶看到真實(shí)(內(nèi)部)服務(wù)器名稱
使用F標(biāo)記強(qiáng)制代理模式內(nèi)部跟蹤遠(yuǎn)程服務(wù)器返回的重定向指令,使用這個(gè)標(biāo)記如果你根本不需要接受遠(yuǎn)程服務(wù)器的重定向指令,在WINHTTP設(shè)置中有重定向限制以避免遠(yuǎn)程重定向循環(huán)
I (ignore case)
不管大小寫強(qiáng)行指定字符匹配
U (Unmangle Log)
當(dāng)URI是源需求而不是重寫需求時(shí)記載URI
O (nOrmalize)
在實(shí)行之前標(biāo)準(zhǔn)化字符串。標(biāo)準(zhǔn)化包括URL-ENCODING,不合法的字符的再移動(dòng)等,這個(gè)標(biāo)記對(duì)于URLS和URLS-ENDODED頭是有用的
CacheClockRate directive
Syntax: CacheClockRate Interval
這個(gè)指令只在GLOBAL配置內(nèi)容中出現(xiàn),如果這個(gè)指令在SITE-LEVEL內(nèi)容中出現(xiàn)將被忽略并把錯(cuò)誤信息寫入httpd.parse.errors 文件
ISAPI_Rewrite caches每次在第一次加載時(shí)配置(http://www.mshuba.com/),使用這個(gè)指令你可以限定當(dāng)一個(gè)特定站點(diǎn)從緩存中清理的不活動(dòng)周期,把這個(gè)參數(shù)設(shè)置的足夠大你可以強(qiáng)制 ISAPI_Rewrite 永不清理緩存,記住任何配置文件的改變將在下次請(qǐng)求后立即更新而忽略這個(gè)周期
Interval
限定特定配置被清理出緩存的不作為時(shí)間(以秒計(jì)),缺省值3600(1小時(shí))
EnableConfig and DisableConfig directives
Syntax:
EnableConfig [SiteID|"Site name"]
DisableConfig [SiteID|"Site name"]
對(duì)所選站點(diǎn)激活或不激活SITE-LEVEL配置或者改變?nèi)笔∨渲?,缺省SITE-LEVEL配置不激活,這個(gè)指令只出現(xiàn)在GLOBAL配置內(nèi)容中
SiteID
Numeric metabase identifier of a site
Site name
Name of the site as it appears in the IIS console
不用參數(shù)使用這個(gè)命令將改變?nèi)笔∨渲玫紼NABLE/DISABLE配置進(jìn)程
例子
下面例子將使配置僅作用于ID=1(典型是缺省站點(diǎn))名字是MY SITE的站點(diǎn)
DisableConfig
EnableConfig 1
EnableConfig”My site”
下邊例子將激活名稱為SOMESITE配置因?yàn)樗指钤O(shè)置重載了缺省設(shè)置
EnableConfig”Some site”
DisableConfig
EnableRewrite and DisableRewrite directives
Syntax:
EnableRewrite [SiteID|"Site name"]
DisableRewrite [SiteID|"Site name"]
對(duì)所選站點(diǎn)激活或不激活重寫或者改變?nèi)笔∨渲?,缺省重寫配置激活,這個(gè)指令只出現(xiàn)在GLOBAL配置內(nèi)容中
SiteID
Numeric metabase identifier of a site
Site name
Name of the site as it appears in the IIS console.
不使用參數(shù)這個(gè)命令將全部激活或者不激活
RepeatLimit directive
Syntax: RepeatLimit Limit
這個(gè)指令可以出現(xiàn)在GLOBAL和SITE-LEVEL配置文件中,如果出現(xiàn)在GLOBAL配置文件中竟改變GLOBAL對(duì)于所有站點(diǎn)的限制,出現(xiàn)在SITE-LEVEL配置中竟只改變對(duì)于這個(gè)站點(diǎn)的限制并且這個(gè)限制不能超過GLOBAL限制
ISAPI_Rewrite在實(shí)行規(guī)則時(shí)允許循環(huán),這個(gè)指令允許限制最大可能循環(huán)的數(shù)量,可以設(shè)置為0或1而不支持循環(huán),
LIMIT
限制最大循環(huán)數(shù)量,缺省32
RFStyle directive
Syntax: RFStyle Old | New
Configuration Utility
ISAPI_Rewrite Full包括配置功用(可以在 ISAPI_Rewrite 程序組中啟動(dòng)),它允許你瀏覽測(cè)試狀態(tài)并輸入注冊(cè)碼(如果在安裝過程中沒有注冊(cè)),并且調(diào)整部分與代理模式操作相關(guān)的產(chǎn)品功能,UTILITY是由三個(gè)頁(yè)面組成的屬性表
Trial page允許你瀏覽TIRAL狀態(tài)并輸入注冊(cè)碼(如果在安裝過程中沒有注冊(cè))
Settings page
這頁(yè)包含對(duì)下列參數(shù)的編輯框
Helper URL
這個(gè)參數(shù)影響過濾器和代理模塊之間的聯(lián)系方式,它即可以是以點(diǎn)做前綴的文件擴(kuò)展名(如 .isrwhlp)也可以是絕對(duì)路徑,
第一種情況下擴(kuò)展名將追加在初始請(qǐng)求URI上并且代理模塊竟通過SCRIPT MAP激活,缺省擴(kuò)展名isrwhlp在安裝進(jìn)程中加在global script map 中,如果你改變這個(gè)擴(kuò)展名或者你的應(yīng)用不繼承g(shù)lobal script map 設(shè)置你應(yīng)該手動(dòng)添加向script map 所需求的入口。這個(gè)應(yīng)該有如下參數(shù)
Executable: An absolute path to the rwhelper.dll in the short form
Extension: Desired extension (.isrwhlp is default)
Verbs radio button: All Verbs
Script engine checkbox: Checked
Check that file exists checkbox: Unchecked
我們已經(jīng)創(chuàng)建了一個(gè)WSH script proxycfg.vbs ,可以簡(jiǎn)單在一個(gè)a script maps中注冊(cè),她位于安裝文件夾并且可以在命令行一如下方式運(yùn)行
cscript proxycfg.vbs [-r] [MetabasePath]
Optional -r 強(qiáng)制注冊(cè)擴(kuò)展名
Optional MetabasePath parameter allows specification of the first metabase key to process. By default it is “/localhost/W3SVC”.
要在所有現(xiàn)存的 script maps 中注冊(cè)你可以以如下命令行激活 script
cscript proxycfg.vbs -r
第二種情況下你應(yīng)該提供一個(gè)URI作為’Helper URL’的值(http://www.gongluela.com/),你也應(yīng)該map 一個(gè) ISAPI_Rewrite的安裝文件夾作為美意個(gè)站點(diǎn)的虛擬文件家
注意:根據(jù)顧客反應(yīng),IIS5(也許包括IIS4)對(duì)長(zhǎng)目錄名有問題。所以我們強(qiáng)烈推薦使用短目錄名
Worker threads limit
這個(gè)參數(shù)限制在代理擴(kuò)展線程池中工作線程數(shù),缺省為0意味著這個(gè)限制等于處理器數(shù)量乘以2
Active threads limit
這個(gè)參數(shù)限制當(dāng)前運(yùn)行線程數(shù),這個(gè)數(shù)量不可大于”Worker threads limit”. 缺省0意思是等于處理器數(shù)量
Queue size 這個(gè)參數(shù)定義最大請(qǐng)求數(shù)量,如果你曾經(jīng)看到Queue timeout expired” 信息在 the Application event log中你可以增加這個(gè)參數(shù)
Queue timeout
這個(gè)參數(shù)定義你在內(nèi)部請(qǐng)求隊(duì)列中防止新請(qǐng)求的最大等待時(shí)間,如果你曾經(jīng)看到Queue timeout expired” 信息在 the Application event log中你可以增加這個(gè)參數(shù)
Connect timeout
以毫秒設(shè)定代理模塊連接超時(shí)
Send timeout
以毫秒設(shè)定代理模塊發(fā)送超時(shí)
Receive timeout
以毫秒設(shè)定代理模塊發(fā)送超時(shí)
About page.
It contains copyright information and a link to the ISAPI_Rewrite’s web site.
Regular expression syntax
這一部分覆蓋了 ISAPI_Rewrite規(guī)定的表達(dá)句法
Literals
所有字符都是原意除了 “.”, “*”, “?”, “+”, “(”, “)”, “{”, “}”, “[", "]“, “^” and “$”.,這些字符在用“\”處理時(shí)是原意,原意指一個(gè)字符匹配自身
Wildcard
The dot character “.” matches any single character except null character and newline character
以下為句法
Repeats
A repeat is an expression that is repeated an arbitrary number of times. An expression followed by “*” can be repeated any number of times including zero. An expression followed by “+” can be repeated any number of times, but at least once. An expression followed by “?” may be repeated zero or one times only. When it is necessary to specify the minimum and maximum number of repeats explicitly, the bounds operator “{}” may be used, thus “a{2}” is the letter “a” repeated exactly twice, “a{2,4}” represents the letter “a” repeated between 2 and 4 times, and “a{2,}” represents the letter “a” repeated at least twice with no upper limit. Note that there must be no white-space inside the {}, and there is no upper limit on the values of the lower and upper bounds. All repeat expressions refer to the shortest possible previous sub-expression: a single character; a character set, or a sub-expression grouped with “()” for example.
Examples:
“ba*” will match all of “b”, “ba”, “baaa” etc.
“ba+” will match “ba” or “baaaa” for example but not “b”.
“ba?” will match “b” or “ba”.
“ba{2,4}” will match “baa”, “baaa” and “baaaa”.
Non-greedy repeats
Non-greedy repeats are possible by appending a ‘?’ after the repeat; a non-greedy repeat is one which will match the shortest possible string.
For example to match html tag pairs one could use something like:
“<\s*tagname[^>]*>(.*?)<\s*/tagname\s*>”
In this case $1 will contain the text between the tag pairs, and will be the shortest possible matching string.
Parenthesis
Parentheses serve two purposes, to group items together into a sub-expression, and to mark what generated the match. For example the expression “(ab)*” would match all of the string “ababab”. All sub matches marked by parenthesis can be back referenced using \N or $N syntax. It is permissible for sub-expressions to match null strings. Sub-expressions are indexed from left to right starting from 1, sub-expression 0 is the whole expression.
Non-Marking Parenthesis
Sometimes you need to group sub-expressions with parenthesis, but don’t want the parenthesis to spit out another marked sub-expression, in this case a non-marking parenthesis (?:expression) can be used. For example the following expression creates no sub-expressions:
“(?:abc)*”
Alternatives
Alternatives occur when the expression can match either one sub-expression or another, each alternative is separated by a “|”. Each alternative is the largest possible previous sub-expression; this is the opposite behaviour from repetition operators.
Examples:
“a(b|c)” could match “ab” or “ac”.
“abc|def” could match “abc” or “def”.
Sets
A set is a set of characters that can match any single character that is a member of the set. Sets are delimited by “[" and "]” and can contain literals, character ranges, character classes, collating elements and equivalence classes. Set declarations that start with “^” contain the compliment of the elements that follow.
Examples:
Character literals:
“[abc]” will match either of “a”, “b”, or “c”.
“[^abc] will match any character other than “a”, “b”, or “c”.
Character ranges:
“[a-z]” will match any character in the range “a” to “z”.
“[^A-Z]” will match any character other than those in the range “A” to “Z”.
Character classes
Character classes are denoted using the syntax “[:classname:]” within a set declaration, for example “[[:space:]]” is the set of all whitespace characters. The available character classes are:
alnum Any alpha numeric character.
alpha Any alphabetical character a-z and A-Z. Other characters may also be included depending upon the locale.
blank Any blank character, either a space or a tab(http://www.xbszgs.com/).
cntrl Any control character.
digit Any digit 0-9.
graph Any graphical character.
lower Any lower case character a-z. Other characters may also be included depending upon the locale.
print Any printable character.
punct Any punctuation character.
space Any whitespace character.
upper Any upper case character A-Z. Other characters may also be included depending upon the locale.
xdigit Any hexadecimal digit character, 0-9, a-f and A-F.
word Any word character – all alphanumeric characters plus the underscore.
unicode Any character whose code is greater than 255, this applies to the wide character traits classes only.
There are some shortcuts that can be used in place of the character classes:
\w in place of [:word:]
\s in place of [:space:]
\d in place of [:digit:]
\l in place of [:lower:]
\u in place of [:upper:]
Collating elements
Collating elements take the general form [.tagname.] inside a set declaration, where tagname is either a single character, or a name of a collating element, for example [[.a.]] is equivalent to [a], and [[.comma.]] is equivalent to [,]. ISAPI_Rewrite supports all the standard POSIX collating element names, and in addition the following digraphs: “ae”, “ch”, “ll”, “ss”, “nj”, “dz”, “lj”, each in lower, upper and title case variations. Multi-character collating elements can result in the set matching more than one character, for example [[.ae.]] would match two characters, but note that [^[.ae.]] would only match one character.
Equivalence classes
Equivalenceclassestakethegeneralform[=tagname=] inside a set declaration, where tagname is either a single character, or a name of a collating element, and matches any character that is a member of the same primary equivalence class as the collating element [.tagname.]. An equivalence class is a set of characters that collate the same, a primary equivalence class is a set of characters whose primary sort key are all the same (for example strings are typically collated by character, then by accent, and then by case; the primary sort key then relates to the character, the secondary to the accentation, and the tertiary to the case). If there is no equivalence class corresponding to tagname, then [=tagname=] is exactly the same as [.tagname.].
To include a literal “-” in a set declaration then: make it the first character after the opening “[" or "[^", the endpoint of a range, a collating element, or precede it with an escape character as in "[\-]“. To include a literal “[" or "]” or “^” in a set then make them the endpoint of a range, a collating element, or precede with an escape character.
Line anchors
An anchor is something that matches the null string at the start(http://www.jiamengku.com/) or end of a line: “^” matches the null string at the start of a line, “$” matches the null string at the end of a line.
Back references
A back reference is a reference to a previous sub-expression that has already been matched, the reference is to what the sub-expression matched, not to the expression itself. A back reference consists of the escape character “\” followed by a digit “1″ to “9″, “\1″ refers to the first sub-expression, “\2″ to the second etc. For example the expression “(.*)\1″ matches any string that is repeated about its mid-point for example “abcabc” or “xyzxyz”. A back reference to a sub-expression that did not participate in any match, matches the null string. In ISAPI_Rewrite all back references are global for entire RewriteRule and corresponding RewriteCond directives. Sub matches are numbered up to down and left to right beginning from the first RewriteCond directive of the corresponding RewriteRule directive, if there is one.
Forward Lookahead Asserts
There are two forms of these; one for positive forward lookahead asserts, and one for negative lookahead asserts:
“(?=abc)” matches zero characters only if they are followed by the expression “abc”.
“(?!abc)” matches zero characters only if they are not followed by the expression “abc”.
Word operators
The following operators are provided for compatibility with the GNU regular expression library.
“\w” matches any single character that is a member of the “word” character class, this is identical to the expression “[[:word:]]”.
“\W” matches any single character that is not a member of the “word” character class, this is identical to the expression “[^[:word:]]”.
“\<” matches the null string at the start of a word.
“\>” matches the null string at the end of the word.
“\b” matches the null string at either the start or the end of a word.
“\B” matches a null string within a word.
Escape operator
The escape character “\” has several meanings.
The escape operator may introduce an operator for example: back references, or a word operator.
The escape operator may make the following character normal, for example “\*” represents a literal “*” rather than the repeat operator.
Single character escape sequences:
The following escape sequences are aliases for single characters:
Escape sequence Character code Meaning
\a 0×07 Bell character.
\t 0×09 Tab character.
\v 0×0B Vertical tab.
\e 0×1B ASCII Escape character.
\0dd 0dd An octal character code, where dd is one or more octal digits.
\xXX 0xXX A hexadecimal character code, where XX is one or more hexadecimal digits.
\x{XX} 0xXX A hexadecimal character code, where XX is one or more hexadecimal digits, optionally a unicode character.
\cZ z-@ An ASCII escape sequence control-Z, where Z is any ASCII character greater than or equal to the character code for ‘@’.
Miscellaneous escape sequences:
The following are provided mostly for perl compatibility, but note that there are some differences in the meanings of \l \L \u and \U:
Escape sequence Meaning
\w Equivalent to [[:word:]].
\W Equivalent to [^[:word:]].
\s Equivalent to [[:space:]].
\S Equivalent to [^[:space:]].
\d Equivalent to [[:digit:]].
\D Equivalent to [^[:digit:]].
\l Equivalent to [[:lower:]].
\L Equivalent to [^[:lower:]].
\u Equivalent to [[:upper:]].
\U Equivalent to [^[:upper:]].
\C Any single character, equivalent to ‘.’.
\X Match any Unicode combining character sequence, for example “a\x 0301″ (a letter a with an acute).
\Q The begin quote operator, everything that follows is treated as a literal character until a \E end quote operator is found.
\E The end quote operator, terminates a sequence begun with \Q.
What gets matched?
The regular expression will match the first possible matching string, if more than one string starting at a given location can match then it matches the longest possible string. In cases where their are multiple possible matches all starting at the same location, and all of the same length, then the match chosen is the one with the longest first sub-expression, if that is the same for two or more matches, then the second sub-expression will be examined and so on. Note that ISAPI_Rewrite uses MATCH algorithm. The result is matched only if the expression matches the whole input sequence. For example:
RewriteCond URL ^/somedir/.* #will match any request to somedir directory and subdirectories, while
RewriteCond URL ^/somedir/ #will match only request to the root of the somedir.
Special note about “pathological” regular expressions
ISAPI_Rewrite uses a very powerful regular expressions engine Regex++ written by Dr. John Maddock. But as any real thing it’s not ideal: There exists some “pathological” expressions which may require exponential time for matching; these all involve nested repetition operators, for example attempting to match the expression “(a*a)*b” against N letter a’s requires time proportional to 2N. These expressions can (almost) always be rewritten in such a way as to avoid the problem, for example “(a*a)*b” could be rewritten as “a*b” which requires only time linearly proportional to N to solve. In the general case, non-nested repeat expressions require time proportional to N2, however if the clauses are mutually exclusive then they can be matched in linear time – this is the case with “a*b”, for each character the matcher will either match an “a” or a “b” or fail, where as with “a*a” the matcher can’t tell which branch to take (the first “a” or the second) and so has to try both.
Boost 1.29.0 Regex++ could detect “pathological” regular expressions and terminate theirs matching. When a rule fails ISAPI_Rewrite sends “500 Internal Server error – Rule Failed” status to a client to indicate configuration error. Also the failed rule is disabled to prevent performance losses
Format string syntax
In format strings, all characters are treated as literals except: “(”, “)”, “$”, “\”, “?”, “:”.
To use any of these as literals you must prefix them with the escape character \
The following special sequences are recognized:
Grouping:
Use the parenthesis characters ( and ) to group sub-expressions within the format string, use \( and \) to represent literal ‘(’ and ‘)’.
Sub-expression expansions:
The following perl like expressions expand to a particular matched sub-expression:
$` Expands to all the text from the end of the previous match to the start of the current match, if there was no previous match in the current operation, then everything from the start of the input string to the start of the match.
$’ Expands to all the text from the end of the match to the end of the input string.
$& Expands to all of the current match.
$0 Expands to all of the current match.
$N Expands to the text that matched sub-expression N.
Conditional expressions:
Conditional expressions allow two different format strings to be selected dependent upon whether a sub-expression participated in the match or not:
?Ntrue_expression:false_expression
Executes true_expression if sub-expression N participated in the match, otherwise executes false_expression.
Example: suppose we search for “(while)|(for)” then the format string “?1WHILE:FOR” would output what matched, but in upper case
Escape sequences:
The following escape sequences are also allowed:
\a The bell character.
\f The form feed character.
\n The newline character.
\r The carriage return character.
\t The tab character.
\v A vertical tab character.
\x A hexadecimal character – for example \x0D.
\x{} A possible unicode hexadecimal character – for example \x{1A0}
\cx The ASCII escape character x, for example \c@ is equivalent to escape-@.
\e The ASCII escape character.
\dd An octal character constant, for example \10
Examples例子
Emulating host-header-based virtual sites on a single site
例如你在兩個(gè)域名注冊(cè)www.gongluela.com 和 www.jiamengku.com,現(xiàn)在你可以創(chuàng)建兩個(gè)不同的站點(diǎn)而使用單一的物理站點(diǎn)。把以下規(guī)則加入到你的httpd.ini 文件
[ISAPI_Rewrite]
#Fix missing slash char on folders
RewriteCond Host: (.*)
RewriteRule ([^.?]+[^.?/]) http\://$1$2/ [I,R]
#Emulate site1
RewriteCond Host: (?:www\.)?site1\.com
RewriteRule (.*) /site1$1 [I,L]
#Emulate site2
RewriteCond Host: (?:www\.)?site2\.com
RewriteRule (.*) /site2$1 [I,L]
現(xiàn)在你可以把你的站點(diǎn)放在/site1 和 /site2 目錄中.
或者你可以應(yīng)用更多的類規(guī)則:
[ISAPI_Rewrite]
#Fix missing slash char on folders
RewriteCond Host: (.*)
RewriteRule ([^.?]+[^.?/]) http\://$1$2/ [I,R]
RewriteCond Host: (www\.)?(.+)
RewriteRule (.*) /$2$3
為站點(diǎn)應(yīng)該命名目錄為 /somesite1.com, /somesite2.info, etc.
Using loops (Next flag) to convert request parameters
假如你希望有物理URL如 http://www.myhost.com/foo.asp?a=A&b=B&c=C 使用請(qǐng)求如 http://www.myhost.com/foo.asp/a/A/b/B/c/C 參數(shù)數(shù)量可以從兩個(gè)請(qǐng)求之間變化
至少有兩個(gè)解決辦法。你可以簡(jiǎn)單的為每一可能的參數(shù)數(shù)量添加一個(gè)分隔規(guī)則或者你可以使用一個(gè)技術(shù)說明如下面的例子
ISAPI_Rewrite]
RewriteRule (.*?\.asp)(\?[^/]*)?/([^/]*)/([^/]*)(.*) $1(?2$2&:\?)$3=$4$5 [NS,I]
這個(gè)規(guī)則將從請(qǐng)求的URL中抽取一個(gè)參數(shù)追加在請(qǐng)求字符的末尾并且從頭重啟規(guī)則進(jìn)程。所以它將循環(huán)直到所有參數(shù)被移動(dòng)到適當(dāng)?shù)奈恢茫蛘咧钡匠^RepeatLimit
也存在許多這個(gè)規(guī)則的變種。但使用不同的分隔字符,例如。使用URLS如http://www.myhost.com/foo.asp~a~A~b~B~c~C 可以應(yīng)中下面的規(guī)則:
ISAPI_Rewrite]
RewriteRule (.*?\.asp)(\?[^~]*)?~([^~]*)~([^~]*)(.*) $1(?2$2&:\?)$3=$4$5 [NS,I]
Running servers behind IIS
假如我們有一個(gè)內(nèi)網(wǎng)服務(wù)器運(yùn)行IIS而幾個(gè)公司服務(wù)器運(yùn)行其他平臺(tái),這些服務(wù)器不能從INTERNET直接進(jìn)入,而只能從我們公司的網(wǎng)絡(luò)進(jìn)入,有一個(gè)簡(jiǎn)單的例子可以使用代理標(biāo)記映射其他服務(wù)器到IIS命名空間:
[ISAPI_Rewrite]
RewriteProxy /mappoint(.+) http\://sitedomain$1 [I,U]
Moving sites from UNIX to IIS
這個(gè)規(guī)則可以幫助你把URL從 /~username 改變到 /username 和從 /file.html 改變到 /file.htm. 這個(gè)在你僅僅把你的站從UNIX移動(dòng)到IIS并且保持搜索引擎和其他外部頁(yè)面對(duì)老頁(yè)面的連接時(shí)是有用的
[ISAPI_Rewrite]
#redirecting to update old links
RewriteRule (.*)\.html $1.htm
RewriteRule /~(.*) http\://myserver/$1 [R]
Moving site location
許多網(wǎng)管問這樣的問題:他們要重定向所有的請(qǐng)求到一個(gè)新的網(wǎng)絡(luò)服務(wù)器,當(dāng)你需要建立一個(gè)更新的站點(diǎn)取代老的的時(shí)候經(jīng)常出現(xiàn)這樣的問題,解決方案是用ISAPI_Rewrite 于老服務(wù)器中
[ISAPI_Rewrite]
#redirecting to update old links
RewriteRule (.+) http\://newwebserver$1 [R]
Browser-dependent content
Dynamically generated robots.txt
robots.txt是一個(gè)搜索引擎用來發(fā)現(xiàn)能不能被索引的文件,但是為一個(gè)大站創(chuàng)建一個(gè)有許多動(dòng)態(tài)內(nèi)容的這個(gè)文件是很復(fù)雜的工作,我們可以寫一個(gè)robots.asp script
現(xiàn)在使用單一規(guī)則生成 robots.txt
[ISAPI_Rewrite]
RewriteRule /robots\.txt /robots.asp
Making search engines to index dynamic pages
站點(diǎn)的內(nèi)容存儲(chǔ)在XML文件中,在服務(wù)器上有一個(gè)/XMLProcess.asp 文件處理XML文件并返回HTML到最終用戶,URLS到文檔有如下形式
http://www.mysite.com/XMLProcess.asp?xml=/somdir/somedoc.xml
但是許多公共引擎不能索引此類文檔,因?yàn)閁RLS包含問號(hào)(文檔動(dòng)態(tài)生成),
ISAPI_Rewrite可以完全消除這個(gè)問題
[ISAPI_Rewrite]
RewriteRule /doc(.*)\.htm /XMLProcess.asp\?xml=$1.xml
現(xiàn)在使用如同http://www.mysite.com/doc/somedir/somedoc.htm的URL進(jìn)入文檔,搜索引擎將不知道不是somedoc.htm 文件并且內(nèi)容是動(dòng)態(tài)生成的
Negative expressions (NOT
有時(shí)當(dāng)模式不匹配你需要應(yīng)用規(guī)則,這種情況下你可以使用在規(guī)則表達(dá)式中稱為Forward Lookahead Asserts
例如你需要不使用IE把所有用戶移動(dòng)到別的地點(diǎn)
[ISAPI_Rewrite]
# Redirect all non Internet Explorer users
# to another location
RewriteCond User-Agent: (?!.*MSIE).*
RewriteRule (.*) /nonie$1
Dynamic authentification
例如我們?cè)谡军c(diǎn)上有一些成員域,我們?cè)谶@個(gè)域上需要密碼保護(hù)文件而我們不喜歡用BUILT-IN服務(wù)器安全,這個(gè)情況下可以建立一個(gè)ASP腳本(稱為proxy.asp),這個(gè)腳本將代理所有請(qǐng)求到成員域并且檢查請(qǐng)求允許,這里有一個(gè)簡(jiǎn)單的模板你可以放進(jìn)你自己的授權(quán)代碼
現(xiàn)在我們要通過配置 ISAPI_Rewrite 通過這個(gè)頁(yè)面代理請(qǐng)求:
[ISAPI_Rewrite]
# Proxy all requests through proxy.asp
RewriteRule /members(.+) /proxy.asp\?http\://mysite.com/members$1
保護(hù)圖片 防止盜鏈
Blocking inline-images (stop hot linking
假設(shè)我們?cè)趆ttp://www.mysite.com/下有些頁(yè)面調(diào)用一些GIF、jpg、png圖片,不允許別人盜鏈引用到他們自己的頁(yè)面上,因?yàn)檫@樣大大增加了服務(wù)器流量。
當(dāng)然我們不能100%保護(hù)圖片,但我們至少可以在得到瀏覽器發(fā)出的HTTP Referer header的地方限制這種情況,因?yàn)檫@個(gè)可以判斷是否我們自己的站點(diǎn)調(diào)用了我們自己的圖片。
[ISAPI_Rewrite]
RewriteCond Host: (.+)
RewriteCond Referer: (?!http://\1.*).*
RewriteRule .*\.(?:gif|jpg|png) /block.gif [I,O]
注:ISAPI_Rewrite分精簡(jiǎn)(Lite)和完全(Full)版.精簡(jiǎn)版不支持對(duì)每個(gè)虛擬主機(jī)站點(diǎn)進(jìn)行重寫,只能進(jìn)行全局處理.不過對(duì)于有服務(wù)器的朋友,精簡(jiǎn)版也就夠啦.精簡(jiǎn)版下載地址:http://www.helicontech.com/download/,就是那Lite Version (free)啦。