APACHE mod_rewrite
基本知識/詞彙定義
重寫條件
RewriteCond
重寫規則
RewriteRule
測試樣式:
CondPattern
用正規表示法建立的測試語句,如^.*.php$就是用來檢查request尾碼是否為.php的測試樣式
測試樣本:
在這邊通常是指所傳入的url, 如http://example.com/xxx/yyy/123.php
參考網站
線上測試站(針對apache開發,與ISAPI的規則未必完全對應)
http://martinmelin.se/rewrite-rule-tester/
apache 文件
http://httpd.apache.org/docs/current/rewrite/rewrite_guide.html
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
isapi文件
http://www.helicontech.com/isapi_rewrite/
http://www.helicontech.com/cms_articles/provocative_SEF_URLs.htm
http://www.helicontech.com/isapi_rewrite/doc/examples.htm
相關文章
http://www.lesishu.com/net/isapi-rewrite-3/
http://www.phpzixue.cn/detail87.shtml
http://www.jb51.net/article/15602.htm
http://ohaha.ks.edu.tw/post/1/47
正規表示法用法簡說
. 代表任意單一字元
ex: ab.c
符合:abbc, abxc, abtc
不符合:abc
(chars) 代表一組單元,(ab)c代表的是一組字元ab,後面跟著c,因此abc符合,但ac,bc不符合
ex: (ab)c
符合:abc
不符合:ac, bc
[set] 字元的集合,有些大陸網站說是測試單元..可能之後有改版?
ex: [ab]c
符合:ac, bc
不符合:abc,cc
[ab]c代表的條件是一各a或b,後面跟著c
[^set] 不符合字元集合
ex: [^ab]c
符合:cc, xc, oc
不符合:ab,bc
text1|text2 可选择的字符串:text1或text2
ex:(aa|bb)c
符合:aac, bbc
不符合:abc
? 檢查測試"單元"是否出現0或1次
ex: (ab)?c
符合:abc, c
不符合:ac,bc,cc
* 檢查測試"單元"是否出現0到無限次
ex: (ab)*c
符合:c, abc, ababc, abababc
不符合:ac, bc, aac, bbc
+ 檢查測試"單元"是否出現1次以上
ex: (ab)+c
符合:abc, ababc
不符合:c
{m,n} 設定測試單元可重複m到n次
ex: (ab){2-3}c
符合:ababc,abababc
不符合:c,abc,abababbc
^ 字串開始標誌
在ISAPI(Apache?)中代表url的開始(不含domain name)
但在指定server變數的情況下,就代表該變數的開始位置
如後面這種用法 RewriteCond %{HTTP:Host} ^www.llllllesishu.cn$
$ 字串結束標誌
\n escape, 脫溢字元(要叫他逃跑字元好像也可以 :p)
Apache_rewrite mod的特殊規則
RewriteCond和RewriteRule共通元素
${mapname:key|default}
設定key-value?
%{VARNAME}
server variables
用於表示伺服器變量
使用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
########################################################
RewriteCond 標示
!
Prefix regular expression with '!' symbol to specify negation pattern.
放在正規表示法測試式前,反轉測試樣式結果?
'<CondPattern'
Treats CondPattern as a plain string that will be lexicographically compared as more than.
將測試樣式看成純文字,檢查測試樣式(文字)的字元順序是否大於測試樣本
'&CondPattern'
Lexicographically less then comparison.
同上,但在測試樣式字元順序小於測試樣本時為真
'=CondPattern'
Lexicographically equals comparison.
測試樣式與測試樣本的字元順序是否相等
'-d'
Test string is existing directory.
測試字串是否為現有目錄名稱
'-f'
Test string is existing file.
測試字串是否指向現有檔案
'-s'
Test string is a file with nonzero size.
測試字串所指向的檔案大小是否非0
'-l'
(link) Unsupported, always false.
不支援此連結,永遠為false
'-x'
(has executable permissions) Unsupported, always true.
(在有執行權限之下)回傳不支援,永遠為true
'-F'
(is existing file, via subrequest) Unsupported, same as '–f'.
(如果檔案存在,且從其他請求而來)回傳不支援,與-f相同
'-U'
(is existing URL, via subrequest) Unsupported, always false.
(是否存在此url,根據其他請求)回傳不支援,永遠為false
‘nocase| NC'
(no case)
忽略大小寫
‘ornext| OR'
(or next condition)
和之後的測試條件
‘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)
添加环境变量
RewriteBase /test
設定接下來的重寫規則中的預設位置
########################################################
RewriteRule
R[=code]
(force redirect)
强制外部重定向
强制在替代字符串加上http://thishost[:thisport]/前缀重定向到外部的URL.
如果code不指定,将用缺省的302 HTTP状态码。
ex: [R=301]//轉向並使用301狀態碼
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) 设置环境变量
$N
rule backreferences
指向第n項規則
%N
RewriteCond backreferences
跳回第幾項條件判斷
2011年3月7日 星期一
APACHE ISAPI rewrite模組簡要說明
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言