一,簡介:
Apache的rewrite模塊,提供了一個(gè)基于正則表達(dá)式規(guī)則的重寫引擎,用來(on the fly)實(shí)時(shí)修改傳入的請(qǐng)求的 URL 。因功能極其強(qiáng)大,被稱為URL重寫的“瑞士軍刀”。
它支持無限的規(guī)則,以及為每個(gè)規(guī)則附加條件,從而提供了一個(gè)真正靈活且強(qiáng)大的 URL 操作機(jī)制。URL 操作可以依賴于各種測試,例如服務(wù)器變量,環(huán)境變量,HTTP 頭,時(shí)間戳,甚至外部數(shù)據(jù)庫查詢等,以便完成 URL 單元匹配。
這個(gè)模塊可以操作完整的 URL (包含目錄信息部分和查詢字符串部分) ,在服務(wù)器上下文 (httpd.conf)、虛擬主機(jī)上下文 (<VirtualHost> 指令塊)、目錄上下文 (.htaccess 文件和 <Directory> 指令塊) 都可以配置。重寫的結(jié)果 URL,可以指向一個(gè)站內(nèi)的處理程序、指向站外的重定向或者一個(gè)站內(nèi)的代理。
既然 mod_rewrite 這么強(qiáng)大,它當(dāng)然是相當(dāng)復(fù)雜。因此,別指望一天之內(nèi)就能看懂整個(gè)模塊。
二,打開Apache的rewrite功能
1、LoadModule
1)在windows環(huán)境下:
打開您的apache安裝目錄“/apache/conf/” 下的 httpd.conf 文件,通過Ctrl+F查找到LoadModule rewrite_module modules/mod_rewrite.so,將前面的”#”號(hào)刪除即可
2)在linux環(huán)境下:
在編譯 apache 的時(shí)候記得加上帶 rewrite 模塊。
2、讓apache服務(wù)器支持.htaccess
在服務(wù)器或者虛擬主機(jī)的<Directory>配置段里,把你的AllowOverride配置設(shè)置成All,表示允許所有指令在 .htaccess 生效。
3、檢查rewrite模塊是否開啟
當(dāng)rewrite模塊已經(jīng)成功加載時(shí),在phpinfo()里可以看到load的模塊列表里有rewrite的名字。
三,特殊字符
1)$N,引用RewriteRule模板中匹配的相關(guān)字串,N表示序號(hào),N=0..9
2)%N,引用最后一個(gè)RewriteCond模板中匹配的數(shù)據(jù),N表示序號(hào)
3)%{VARNAME},服務(wù)器變量
4)${mapname:key|default},映射函數(shù)調(diào)用
四,指令
Apache Rewrite 的重寫規(guī)則的具體指令共有 RewriteBase, RewriteCond, RewriteEngine, RewriteLock, RewriteLog, RewriteLogLevel, RewriteMap, RewriteOptions, RewriteRule 九個(gè)指令。
下面我們就最常用的RewriteEngine, RewriteBase, RewriteCond, RewriteRule這四個(gè)指令重點(diǎn)講解。
1、RewriteEngine指令
| 原文 | 譯文 | |
|---|---|---|
| 描述(Description) | Enables or disables runtime rewriting engine | 開啟或關(guān)閉重寫引擎 |
| 語法(Syntax) | RewriteEngine on|off | |
| 默認(rèn)(Default) | RewriteEngine off | |
| 作用域/上下文(Context) | server config, virtual host, directory, .htaccess |
2、RewriterRule規(guī)則
一條RewriteRule指令,定義一條重寫規(guī)則,規(guī)則間的順序非常重要。對(duì)Apache1.2及以后的版本,模板(pattern)是一個(gè) POSIX正則式,用以匹配當(dāng)前的URL。當(dāng)前的URL不一定是用記最初提交的URL,因?yàn)榭赡苡靡恍┮?guī)則在此規(guī)則前已經(jīng)對(duì)URL進(jìn)行了處理。
| 原文 | 譯文 | |
|---|---|---|
| 描述(Description) | Defines rules for the rewriting engine | |
| 語法(Syntax) | RewriteRule Pattern Substitution [Flag1,Flag2,Flag3] | |
| 作用域/上下文(Context) | server config, virtual host, directory, .htaccess |
指令說明:匹配部分(Pattern) 是正則匹配URL的正則表達(dá)式(注意特殊字符需要轉(zhuǎn)義處理), 可以在替換部分(Substitution)使用反向引用匹配部分的內(nèi)容. 引用模式為: $N (N為1-9的整數(shù))。
先說明一下一個(gè)比較特別的 Substitution 值: "-", 如果Substitution是 "-" 的話, 那么被請(qǐng)求的URL不會(huì)被修改掉,只做匹配檢查。
在URL重寫的匹配部分中,服務(wù)器會(huì)把請(qǐng)求的URL的一部分刪除掉再傳遞給Pattern部分進(jìn)行匹配,重寫結(jié)束后再添加上去。所以平常我們看到的匹配規(guī)則總是不帶網(wǎng)址前面的那些域名的什么東西的,也不帶什么目錄什么的,這些 apache已經(jīng)給刪掉了,處理完后再加到前面。但是有個(gè)例外,就是如果 Substitution 部分是帶 http:// 開頭的話, 那就直接重定向了,服務(wù)器不會(huì)把先前刪除的再給加上了,不然就出錯(cuò)了。
查看幾個(gè)官方的例子:
例如請(qǐng)求地址為: http://thishost/somepath/pathinfo, 看下面幾種結(jié)果
| Given Rule (重寫規(guī)則) | Resulting Substitution (替換結(jié)果) |
|---|---|
| ^/somepath(.*) otherpath$1 | invalid, not supported(無效的重寫語句,不支持) |
| ^/somepath(.*) otherpath$1 [R] | invalid, not supported |
| ^/somepath(.*) otherpath$1 [P] | invalid, not supported |
| ^/somepath(.*) /otherpath$1 | /otherpath/pathinfo |
| ^/somepath(.*) /otherpath$1 [R] | http://thishost/otherpath/pathinfo via external redirection(外部重定向) |
| ^/somepath(.*) /otherpath$1 [P] | doesn't make sense, not supported |
| ^/somepath(.*) http://thishost/otherpath$1 | http://www.test.com/otherpath/pathinfo |
| ^/somepath(.*) http://thishost/otherpath$1 [R] | http://thishost/otherpath/pathinfo via external redirection(外部重定向) |
| ^/somepath(.*) http://thishost/otherpath$1 [P] | doesn't make sense, not supported |
| ^/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)(外部重定向,[R] 標(biāo)志是多余的) |
| ^/somepath(.*) http://otherhost/otherpath$1 [P] | http://otherhost/otherpath/pathinfo via internal proxy(內(nèi)部網(wǎng)關(guān)重定向) |