Apache mod_rewrite規則重寫的標誌一覽

轉自:http://www.jb51.net/article/15602.htm

Apache mod_rewrite規則重寫的標誌一覽 
R[=code](force redirect) 強制外部重定向 
強制在替代字符串加上http://thishost[:thisport]/前綴重定向到外部的URL.如果code不指定,將用缺省的302 HTTP狀態碼。 
F(force URL to be forbidden)禁用URL,返回403HTTP狀態碼。 
G(force URL to be gone) 強制URL爲GONE,返回410HTTP狀態碼。 
P(force proxy) 強制使用代理轉發。 
L(last rule) 表明當前規則是最後一條規則,停止分析以後規則的重寫。 
N(next round) 重新從第一條規則開始運行重寫過程。 
C(chained with next rule) 與下一條規則關聯 
如果規則匹配則正常處理,該標誌無效,如果不匹配,那麼下面所有關聯的規則都跳過。 
T=MIME-type(force MIME type) 強制MIME類型 
NS (used only if no internal sub-request) 只用於不是內部子請求 
NC(no case) 不區分大小寫 
QSA(query string append) 追加請求字符串 
NE(no URI escaping of output) 不在輸出轉義特殊字符 
例如:RewriteRule /foo/(.*) /bar?arg=P1/%3d$1 [R,NE] 將能正確的將/foo/zoo轉換成/bar?arg=P1=zed 
PT(pass through to next handler) 傳遞給下一個處理 
例如: 
RewriteRule ^/abc(.*) /def$1 [PT] # 將會交給/def規則處理 
Alias /def /ghi 
S=num(skip next rule(s)) 跳過num條規則 
E=VAR:VAL(set environment variable) 設置環境變量 
使用mod_rewrite時常用的服務器變量: 
HTTP headers:HTTP_USER_AGENT, HTTP_REFERER, HTTP_COOKIE, HTTP_HOST, HTTP_ACCEPT 
connection & request: REMOTE_ADDR, QUERY_STRING 
server internals: DOCUMENT_ROOT, SERVER_PORT, SERVER_PROTOCOL 
system stuff: TIME_YEAR, TIME_MON, TIME_DAY 
RewriteRule規則表達式的說明: 
. 匹配任何單字符 
[chars] 匹配字符串:chars 
[^chars] 不匹配字符串:chars 
text1|text2 可選擇的字符串:text1或text2 
? 匹配0到1個字符 
* 匹配0到多個字符 
+ 匹配1到多個字符 
^ 字符串開始標誌 
$ 字符串結束標誌 
/n 轉義符標誌 
反向引用 $N 用於 RewriteRule 中匹配的變量調用(0 <= N <= 9) 
反向引用 %N 用於 RewriteCond 中最後一個匹配的變量調用(1 <= N <= 9) 
RewriteCond適用的標誌符 
‘nocase|NC' (no case)忽略大小 
‘ornext|OR' (or next condition)邏輯或,可以同時匹配多個RewriteCond條件 
RewriteRule適用的標誌符 
‘redirect|R [=code]' (force redirect)強迫重寫爲基於http開頭的外部轉向(注意URL的變化) 如:[R=301,L] 
‘forbidden|F' (force URL to be forbidden)重寫爲禁止訪問 
‘proxy|P' (force proxy)重寫爲通過代理訪問的http路徑 
‘last|L' (last rule)最後的重寫規則標誌,如果匹配,不再執行以後的規則 
‘next|N' (next round)循環同一個規則,直到不能滿足匹配 
‘chain|C' (chained with next rule)如果匹配該規則,則繼續下面的有Chain標誌的規則。 
‘type|T=MIME-type' (force MIME type)指定MIME類型 
‘nosubreq|NS' (used only if no internal sub-request)如果是內部子請求則跳過 
‘nocase|NC' (no case)忽略大小 
‘qsappend|QSA' (query string append)附加查詢字符串 
‘noescape|NE' (no URI escaping of output)禁止URL中的字符自動轉義成%[0-9]+的形式。 
‘passthrough|PT' (pass through to next handler)將重寫結果運用於mod_alias 
'skip|S=num' (skip next rule(s))跳過下面幾個規則 
‘env|E=VAR:VAL' (set environment variable)添加環境變量 
實戰 
例子: 
RewriteEngine on 
RewriteCond %{HTTP_USER_AGENT} ^MSIE [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} ^Opera [NC] 
RewriteRule ^.* - [F,L] 這裏”-”表示沒有替換,瀏覽器爲IE和Opera的訪客將被禁止訪問。 
例子: 
RewriteEngine On 
RewriteBase /test 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ([^/]+)$ /test/$1.php 
#for example: /test/admin => /test/admin.php 
RewriteRule ([^/]+)/.html$ /test/$1.php [L] 
#for example: /test/admin.html => /test/admin.php 
限制目錄只能顯示圖片 
< IfModule mod_rewrite.c> 
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !^.*/.(gif|jpg|jpeg|png|swf)$ 
RewriteRule .*$ - [F,L] 
< /IfModule>

發佈了115 篇原創文章 · 獲贊 3 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章