apache .haccess重寫規則及參數


Apache模塊 mod_rewrite
服務器變量: 引用方法是
%{ NAME_OF_VARIABLE }
NAME_OF_VARIABLE可以是下表列出的字符串之一: HTTP headers: connection & request: 
HTTP_USER_AGENT
HTTP_REFERER
HTTP_COOKIE
HTTP_FORWARDED
HTTP_HOST
HTTP_PROXY_CONNECTION
HTTP_ACCEPT
REMOTE_ADDR
REMOTE_HOST
REMOTE_USER
REMOTE_IDENT
REQUEST_METHOD
SCRIPT_FILENAME
PATH_INFO
QUERY_STRING
AUTH_TYPE
 
server internals: system stuff: specials:
DOCUMENT_ROOT
SERVER_ADMIN
SERVER_NAME
SERVER_ADDR
SERVER_PORT
SERVER_PROTOCOL
SERVER_SOFTWARE
TIME_YEAR
TIME_MON
TIME_DAY
TIME_HOUR
TIME_MIN
TIME_SEC
TIME_WDAY
TIME
API_VERSION
THE_REQUEST
REQUEST_URI
REQUEST_FILENAME
IS_SUBREQ
謹記: CondPattern是一個兼容perl的正則表達式, 但是還有若干增補:
可以在pattern串中使用’!’ 字符(驚嘆號)來實現匹配的反轉。
CondPatterns有若干特殊的變種。除了正則表達式的標准用法,還有下列用法:
’;CondPattern’ (詞典順序的大於)
將CondPattern視為純字符串,與TestString以詞典順序相比較. 如果按詞典順序,TestString大於CondPattern,則為真.
’=CondPattern’ (詞典順序的等於)
將CondPattern視為純字符串,與TestString以詞典順序相比較. 如果按詞典順序,TestString等於CondPattern,則為真,即, 兩個字符串(逐個字符地)完全相等。如果CondPattern只是\"\"(兩個引號), 則TestString將與空串相比較.
’-d’ (是一個目錄[directory])
     將TestString視為一個路徑名並測試它是否存在而且是一個目錄.
’-f’ (是一個常規的文件[file])
     將TestString視為一個路徑名並測試它是否存在而且是一個常規的文件.
’-s’ (是一個非空的常規文件[size])
     將TestString視為一個路徑名並測試它是否存在而且是一個尺寸大於0的常規的文件.
’-l’ (是一個符號連接[link])
     將TestString視為一個路徑名並測試它是否存在而且是一個符號連接.
’-F’ (對子請求有效的業已存在的文件)
     測試TestString是否一個有效的文件, 而且可以被服務器當前已經配置的所有存取控制所存取。 它用一個內部子請求來做判斷,由於會降低服務器的性能,請小心使用!
’-U’ (對子請求有效的業已存在的URL)
     測試TestString是否一個有效的URL, 而且可以被服務器當前已經配置的所有存取控制所存取。 它用一個內部子請求來做判斷,由於會降低服務器的性能,請小心使用!
注意
所有這些測試都可以用驚嘆號作前綴(’!’)以實現條件的反轉.
另外,還可以為CondPattern追加特殊的標記
[flags]
作為RewriteCond指令的第三個參數。 Flags是一個以逗號分隔的以下標記的列表:
’nocase|NC’ (no case)
      它使測試忽略大小寫, 即, 擴展后的TestString和CondPattern中, ’A-Z’ 和’a-z’是沒有區別的。此標記僅作用於TestString和CondPattern的比較, 而對文件系統和子請求的測試不起作用。
’ornext|OR’ (or next condition)
      它以OR方式組合若干規則的條件,而不是隱含的AND。典型的例子如下:
RewriteCond %{REMOTE_HOST}  ^host1.*  [OR]
RewriteCond %{REMOTE_HOST}  ^host2.*  [OR]
RewriteCond %{REMOTE_HOST}  ^host3.*
RewriteRule ...some special stuff for any of these hosts...
舉例:
如果要按請求頭中的``User-Agent:’重寫一個站點的主頁,可以這樣寫:
RewriteCond  %{HTTP_USER_AGENT}  ^Mozilla.*
RewriteRule  ^/$                 /homepage.max.html  [L]
RewriteCond  %{HTTP_USER_AGENT}  ^Lynx.*
RewriteRule  ^/$                 /homepage.min.html  [L]
RewriteRule  ^/$                 /homepage.std.html  [L]

RewriteEngine 指令
      RewriteEngine off    使用該指令可以使mod_rewrite模塊無效
RewriteLog 指令
      RewriteLog \"/usr/local/var/apache/logs/rewrite.log\"
      或者 RewriteLog logs/rewrite.log
RewriteLogLevel 指令
      RewriteLogLevel 0     關閉所有的重寫操作記錄。
      使用較高的Level值會使Apache服務器速度急劇下降!
      重寫日志使用大於2的Level值只用於調試!
      舉例:     RewriteLogLevel 3
RewriteRule 指令
Text:
   .           Any single character
   [chars]     Character class: One  of chars
   [^chars]    Character class: None of chars
   text1|text2 Alternative: text1 or text2
Quantifiers:
   ?           0 or 1 of the preceding text
   *           0 or N of the preceding text (N >; 0)
   +           1 or N of the preceding text (N >; 1)
Grouping:
   (text)      Grouping of text
               (either to set the borders of an alternative or
               for making backreferences where the Nth group can
               be used on the RHS of a RewriteRule with $N)
Anchors:
   ^           Start of line anchor
   $           End   of line anchor
Escaping:
   \char       escape that particular char
               (for instance to specify the chars \".[]()\" etc.)
在mod_rewrite中,還可以使用否字符(’!’)的pattern前綴,以實現pattern的反轉。
$N 反向引用RewriteRule的pattern
%N 反向引用最后匹配的RewriteCond pattern
規則條件測試字符串中(%{VARNAME})的服務器變量
映射函數調用(${mapname:key|default})
[flags]
作為RewriteRule指令的第三個參數。 Flags是一個包含以逗號分隔的下列標記的列表:
[R]  (強制重定向 redirect)
[F]  (強制URL為被禁止的 forbidden)
     立即反饋一個HTTP響應代碼403(被禁止的)。
[G]  (強制URL為已廢棄的 gone)
     立即反饋一個HTTP響應代碼410(已廢棄的)
[P]  (強制為代理 proxy)
     並立即把處理移交給代理模塊。
[L]  (最后一個規則 last)
     立即停止重寫操作,並不再應用其他重寫規則。
[N]  (重新執行 next round)
     重新執行重寫操作(從第一個規則重新開始).
[C]  (與下一個規則相鏈接 chained)
     此標記使當前規則與下一個(其本身又可以與其后繼規則相鏈接的, 並可以如此反復的)規則相鏈接。 它產生這樣一個效果: 如果一個規則被匹配,通常會繼續處理其后繼規則, 即,這個標記不起作用;如果規則不能被匹配, 則其后繼的鏈接的規則會被忽略。比如,在執行一個外部重定向時, 對一個目錄級規則集,你可能需要刪除``.www’’ (此處不應該出現``.www’’的)。
[]’type|T=MIME-type’ (強制MIME類型 type)
    強制目標文件的MIME類型為MIME-type。 比如,它可以用於模擬mod_alias中的ScriptAlias指令, 以內部地強制被映射目錄中的所有文件的MIME類型為``application/x-httpd-cgi’’.
[NS] (僅用於不對內部子請求進行處理 no internal sub-request)
     在當前請求是一個內部子請求時,此標記強制重寫引擎跳過該重寫規則。
[NC] (忽略大小寫 no case)
     它使Pattern忽略大小寫.
[QSA] (追加請求串 query string append)
     此標記強制重寫引擎在已有的替換串中追加一個請求串,而不是簡單的替換。
[NE] (在輸出中不對URI作轉義 no URI escaping)
     此標記阻止mod_rewrite對重寫結果應用常規的URI轉義規則。
     例:RewriteRule /foo/(.*) /bar?arg=P1\%3d$1 [R,NE]
        可以使’/foo/zed’轉向到一個安全的請求’/bar?arg=P1=zed’.
[PT] (移交給下一個處理器 pass through)
     此標記強制重寫引擎將內部結構request_rec中的uri字段設置為 filename字段的值,它只是一個小修改,使之能對來自其他URI到文件名翻譯器的 Alias,ScriptAlias, Redirect 等指令的輸出進行后續處理。舉一個能說明其含義的例子: 如果要通過mod_rewrite的重寫引擎重寫/abc為/def, 然后通過mod_alias使/def轉變為/ghi,可以這樣:
RewriteRule ^/abc(.*) /def$1 [PT]
Alias /def /ghi
如果省略了PT標記,雖然mod_rewrite運作正常, 即, 作為一個使用API的URI到文件名翻譯器, 它可以重寫uri=/abc/...為filename=/def/..., 但是,后續的mod_alias在試圖作URI到文件名的翻譯時,則會失效。
注意: 如果需要混合使用不同的包含URI到文件名翻譯器的模塊時, 就必須使用這個標記。。 混合使用mod_alias和mod_rewrite就是個典型的例子。
’skip|S=num’ (跳過后繼的規則 skip)
此標記強制重寫引擎跳過當前匹配規則后繼的num個規則。 它可以實現一個偽if-then-else的構造: 最后一個規則是then從句,而被跳過的skip=N個規則是else從句. (它和’chain|C’標記是不同的!)
’env|E=VAR:VAL’ (設置環境變量 environment variable)
此標記使環境變量VAR的值為VAL, VAL可以包含可擴展的反向引用的正則表達式$N和%N。 此標記可以多次使用以設置多個變量。 這些變量可以在其后許多情況下被間接引用,但通常是在XSSI (via ;) or CGI (如 $ENV{’VAR’})中, 也可以在后繼的RewriteCond指令的pattern中通過%{ENV:VAR}作引用。 使用它可以從URL中剝離並記住一些信息。
’cookie|CO=NAME:VAL:domain[:lifetime[]]’ (設置cookie)
它在客戶端瀏覽器上設置一個cookie。 cookie的名稱是NAME,其值是VAL。 domain字段是該cookie的域,比如’.apache.org’, 可選的lifetime是cookie生命期的分鍾數, 可選的path是cookie的路徑。
注意
絕不要忘記,在服務器級配置文件中,Pattern是作用於整個URL的。 但是在目錄級配置文件中, (一般總是和特定目錄名稱相同的)目錄前綴會在pattern匹配時被自動刪除, 而又在替換完畢后自動被加上。此特性對很多種重寫是必須的,因為, 如果沒有這個剝離前綴的動作,就必須與其父目錄去匹配,而這並不總是可行的。
但是有一個例外: 如果替換串以``http://’’開頭, 則不會附加目錄前綴, 而是強制產生一個外部重定向,或者(如果使用了P標記)是一個代理操作!
注意
為了對目錄級配置啟用重寫引擎,你必須在這些文件中設置``RewriteEngine On’’, 並且打開``Options FollowSymLinks’。 如果管理員對用戶目錄禁用了FollowSymLinks, 則無法使用重寫引擎。這個限制是為了安全而設置的。
以下是所有可能的替換組合及其含義:
在服務器級配置中(httpd.conf)
,對這樣一個請求 ``GET /somepath/pathinfo’’:
Given Rule                                      Resulting Substitution
----------------------------------------------  ----------------------------------
^/somepath(.*) otherpath$1                      not supported, because invalid!
^/somepath(.*) otherpath$1  [R]                 not supported, because invalid!
^/somepath(.*) otherpath$1  [P]                 not supported, because invalid!
----------------------------------------------  ----------------------------------
^/somepath(.*) /otherpath$1                     /otherpath/pathinfo
^/somepath(.*) /otherpath$1 [R]                 http://thishost/otherpath/pathinfo
                                                 via external redirection
^/somepath(.*) /otherpath$1 [P]                 not supported, because silly!
----------------------------------------------  ----------------------------------
^/somepath(.*) http://thishost/otherpath$1      /otherpath/pathinfo
^/somepath(.*) http://thishost/otherpath$1 [R]  http://thishost/otherpath/pathinfo
                                                 via external redirection
^/somepath(.*) http://thishost/otherpath$1 [P]  not supported, because silly!
----------------------------------------------  ----------------------------------
^/somepath(.*) http://otherhost/otherpath$1     http://otherhost/otherpath/pathinfo
                                                 via external redirection
^/somepath(.*) http://otherhost/otherpath$1 [R] http://otherhost/otherpath/pathinfo
                                                 via external redirection
                                                 (the [R] flag is redundant)
^/somepath(.*) http://otherhost/otherpath$1 [P] http://otherhost/otherpath/pathinfo
                                                 via internal proxy
在/somepath的目錄級配置中
(即, 目錄/physical/path/to/somepath的.htaccess文件中包含 RewriteBase /somepath)
對這樣一個請求``GET /somepath/localpath/pathinfo’’:
Given Rule                                      Resulting Substitution
----------------------------------------------  ----------------------------------
^localpath(.*) otherpath$1                      /somepath/otherpath/pathinfo
^localpath(.*) otherpath$1  [R]                 http://thishost/somepath/otherpath/pathinfo
                                                 via external redirection
^localpath(.*) otherpath$1  [P]                 not supported, because silly!
----------------------------------------------  ----------------------------------
^localpath(.*) /otherpath$1                     /otherpath/pathinfo
^localpath(.*) /otherpath$1 [R]                 http://thishost/otherpath/pathinfo
                                                 via external redirection
^localpath(.*) /otherpath$1 [P]                 not supported, because silly!
----------------------------------------------  ----------------------------------
^localpath(.*) http://thishost/otherpath$1      /otherpath/pathinfo
^localpath(.*) http://thishost/otherpath$1 [R]  http://thishost/otherpath/pathinfo
                                                 via external redirection
^localpath(.*) http://thishost/otherpath$1 [P]  not supported, because silly!
----------------------------------------------  ----------------------------------
^localpath(.*) http://otherhost/otherpath$1     http://otherhost/otherpath/pathinfo
                                                 via external redirection
^localpath(.*) http://otherhost/otherpath$1 [R] http://otherhost/otherpath/pathinfo
                                                 via external redirection
                                                 (the [R] flag is redundant)
^localpath(.*) http://otherhost/otherpath$1 [P] http://otherhost/otherpath/pathinfo
                                                 via internal proxy
舉例:
要重寫這種形式的URL
/ Language /~ Realname /.../ File

/u/ Username /.../ File . Language
可以把這樣的對應關系保存在/path/to/file/map.txt映射文件中, 此后,只要在Apache服務器配置文件中增加下列行,即可:
RewriteLog   /path/to/file/rewrite.log
RewriteMap   real-to-user               txt:/path/to/file/map.txt
RewriteRule  ^/([^/]+)/~([^/]+)/(.*)$   /u/${real-to-user:$2|nobody}/$3.$1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM