Apache Rewrite規則詳解

1Rewrite規則簡介:

 

Rewirte主要的功能就是實現URL的跳轉,它的正則表達式是基於Perl語言。可基於服務器級的(httpd.conf)和目錄級的(.htaccess)兩種方式。如果要想用到rewrite模塊,必須先安裝或加載rewrite模塊。方法有兩種一種是編譯apache的時候就直接安裝rewrite模塊,別一種是編譯apache時以DSO模式安裝apache,然後再利用源碼和apxs來安裝rewrite模塊。

 

基於服務器級的(httpd.conf)有兩種方法,一種是在httpd.conf的全局下直接利用RewriteEngine on來打開rewrite功能;另一種是在局部裏利用RewriteEngineon來打開rewrite功能,下面將會舉例說明,需要注意的是,必須在每個virtualhost裏用RewriteEngine on來打開rewrite功能。否則virtualhost裏沒有RewriteEngine on它裏面的規則也不會生效。

 

基於目錄級的(.htaccess),要注意一點那就是必須打開此目錄的FollowSymLinks屬性且在.htaccess裏要聲明RewriteEngine on

2ApacheURL重寫規則的標誌詳細說明

1) R[=code](force redirect) 強制外部重定向

強制在替代字符串加上http://thishost[:thisport]/前綴重定向到外部的URL.如果code不指定,將用缺省的302 HTTP狀態碼。

 

2) F(force URL to be forbidden)禁用URL,返回403HTTP狀態碼。

3) G(force URL to be gone) 強制URLGONE,返回410HTTP狀態碼。

4) P(force proxy) 強制使用代理轉發。

5) L(last rule) 表明當前規則是最後一條規則,停止分析以後規則的重寫。

6) N(next round) 重新從第一條規則開始運行重寫過程。

7) C(chained with next rule) 與下一條規則關聯

 

如果規則匹配則正常處理,該標誌無效,如果不匹配,那麼下面所有關聯的規則都跳過。

 

8) T=MIME-type(force MIME type) 強制MIME類型

9) NS (used only if no internalsub-request) 只用於不是內部子請求

10) NC(no case) 不區分大小寫

11) QSA(query string append) 追加請求字符串

12) NE(no URI escaping of output) 不在輸出轉義特殊字符

例如:RewriteRule/foo/(.*) /bar?arg=P1%3d$1 [R,NE]將能正確的將/foo/zed轉換成/bar?arg=P1=zed

13) PT(pass through to next handler) 傳遞給下一個處理

例如:

RewriteRule ^/abc(.*) /def$1 [PT] # 將會交給/def規則處理

Alias /def /ghi

14) S=num(skip next rule(s)) 跳過num條規則

15) E=VAR:VAL(set environment variable) 設置環境變量

3Rewrite規則表達式的說明:

. 匹配任何單字符

[chars] 匹配字符串:chars

[^chars] 不匹配字符串:chars

text1|text2 可選擇的字符串:text1text2

? 匹配01個字符

* 匹配0到多個字符

+ 匹配1到多個字符

^ 字符串開始標誌

$ 字符串結束標誌

\n 轉義符標誌

 

反向引用 $N 用於RewriteRule 中匹配的變量調用(0 <= N <= 9)$N引用緊跟在RewriteCond後面的RewriteRule中模板中的括號中的模板在當前URL中匹配的數據。

反向引用 %N 用於RewriteCond 中最後一個匹配的變量調用(1 <= N <= 9)%N引用最後一個RewriteCond的模板中的括號中的模板在當前URL中匹配的數據。

 

 

RewriteCond——定義重寫發生的條件,可以理解爲“篩選條件”

RewriteRule——爲重寫引擎定義重寫規則

RewriteRule^(.*)$  http://www.b.com$1 [R,L]

上文中的意思是將匹配過的信息轉換成www.b.com$1意思是“正則表達式中的域/參數傳遞”

 

RewriteLog /tmp/rewrite.log

RewriteLogLevel 3

日誌存儲位置和日誌的記錄等級(最高爲8級)

 

4、舉例說明:

 

例一.client請求的主機前綴不是www.qq.cn192.168.15.20都跳轉到主機前綴爲http://www.qq.cn,避免相同內容的網頁有多個指向的域名,如http://qq.cn

NameVirtualHost *:80

<VirtualHost *:80>

   ServerAdmin [email protected]

   DocumentRoot "/application/apache2.2.17/htdocs"

   ServerName www.qq.cn

    ErrorLog "logs/www.qq.cn-error_log"

   CustomLog "|/usr/local/sbin/cronolog/application/apache/logs/%Y/%m/%d/access_www.qq.cn_%Y%m%d%H.log" combinedenv=!IMAG

</VirtualHost>

<VirtualHost *:80>

   ServerAdmin [email protected]

   #DocumentRoot "/application/apache2.2.17/htdocs"

   ServerName qq.cn

    #----------RewriteRule--start------------------------#

    RewriteEngine on

RewriteCond %{HTTP_HOST} !^www.qq.cn[NC]

RewriteCond %{HTTP_HOST} !^192.168.15.20[NC] #這一條可以不加

    RewriteRule ^/(.*)http://www.qq.cn/$1 [L]

    #--------------------------------------------------------#

   ErrorLog "logs/dn3s-error_log"

   CustomLog "|/usr/local/sbin/cronolog/application/apache/logs/%Y/%m/%d/access_%Y%m%d%H.log" combined env=!IMAG

</VirtualHost>

說明:

RewriteEngine on #打開rewirte功能

RewriteCond %{HTTP_HOST} !^www.qq.cn [NC] #聲明Client請求的主機中前綴不是www.kiya.cn,其中 [NC] 的意思是忽略大小寫

RewriteCond %{HTTP_HOST} !^192.168.15.20[NC] #聲明Client請求的主機中前綴不是192.168.15.20,其中 [NC] 的意思是忽略大小寫

RewriteCond %{HTTP_HOST} !^$ #聲明Client請求的主機中前綴不爲空

RewriteRule ^(.*) http://www.qq.cn/ [L] #含義是如果Client請求的主機中的前綴符合上述條件,則直接進行跳轉到http://www.qq.cn/,[L]意味着立即停止重寫操作,並不再應用其他重寫規則。這裏的.*是指匹配所有URL中不包含換行字符,()括號的功能是把所有的字符做一個標記,以便於後面的應用.就是引用前面裏的(.*)字符。

 

例二、將輸入 123.dn3s.cn 的域名時跳轉到www.dn3s.cn

RewriteEngine on

RewriteCond %{HTTP_HOST} ^123.dn3s.cn [NC]

RewriteRule ^(.*) http://www.dn3s.cn/ [L]

 

例三、賽卡軟件近期更換了域名,新域名爲www.sicasoft.com, 更加簡短好記。這時需要將原來的域名ss.kiya.cn,以及論壇所在地址ss.kiya.cn/bbs/定向到新的域名,以便用戶可以找到,並且使原來的論壇URL 繼續有效而不出現 404 未找到,比如原來的http://ss.kiya.cn/bbs/tread-60.html,讓它在新的域名下繼續有效,點擊後轉發到http://bbs.sicasoft.com/tread-60.html,而其他網頁,如原先的http://ss.kiya.cn/purchase不會到二級域名bbs.sicasoft.com/purchase上,而是到www.sicasoft.com/purchase

重定向規則應該這樣寫:

 

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/bbs/

RewriteRule ^bbs/(.*)http://bbs.sicasoft.com/$1 [R=permanent,L]

RewriteCond %{REQUEST_URI} !^/bbs/

RewriteRule ^(.*)http://www.sicasoft.com/$1 [R=permanent,L]

 

說明:

在這裏判斷當前服務器變量REQUEST_URI是否等於bbs爲真就進行重寫,按照R=permanent進行永久重定向,L表示並立即停止重寫操作,並不再應用其他重寫規則

 

例四、

1.用http://www.zzz.com/xxx.php 來訪問 http://www.zzz.com/xxx/

2.用http://yyy.zzz.com 來訪問 http://www.zzz.com/user.php?username=yyy 的功能

 

RewriteEngine On

RewriteCond%{HTTP_HOST} ^www.zzz.com

RewriteCond%{REQUEST_URI} !^user.php$

RewriteCond%{REQUEST_URI} \.php$

RewriteRule(.*).php$ http://www.zzz.com/$1/ [R]

RewriteCond%{HTTP_HOST} !^www.zzz.com

RewriteRule ^(.+)%{HTTP_HOST} [C]

RewriteRule^([^.]+).zzz.com http://www.zzz.com/user.php?username=$1

 

例五、

/type.php?typeid=*--> /type*.html

/type.php?typeid=*&page=*--> /type*page*.html

 

RewriteRule^/type([0-9]+).html$ /type.php?typeid=$1 [PT]

RewriteRule^/type([0-9]+)page([0-9]+).html$ /type.php?typeid=$1&page=$2 [PT]

生產案例:

例六、

   #--------------www.qq.comor www.qq.com/index.html-----------------

   RewriteCond%{REQUEST_URI}  ^/$ [NC,OR]

   RewriteCond%{REQUEST_URI}  ^/index\.(html|htm)$ [NC]

   RewriteRule^/(.*)$ /cmshtdocs/shouye/index.html [PT,NC,L]

例七、

  #--------------Ifit isn't doc page request, it will be forwarded to maidong--------

  RewriteCond%{REQUEST_URI}  !^/wiki/(.+)$ [NC]

  RewriteRule^/(.+)$ http://maidong.qq.com/$1 [P,NC,L]

例八、

   #------------Ifit is a doc page request for test, it will be forwarded to php module-------

   RewriteRule^/wiki/ts/(.+)$ /ts.php?c=wikdoc&t=$1

  

   #------------Ifit is a doc page request, it will be forwarded to php module-------

   RewriteRule^/wiki/(.+)$ /transfer.php?c=wikdoc&t=$1     

例九、                                                                          

RewriteEngine on

 

 #RewriteCond %{http_host} ^xuyuan.qq.com$ [NC]

 #RewriteRule (.*) http://xuyuan.qq/flow_entrance/index.html[R=301,NC,NE,L]

 

  #bd

 RewriteCond %{REQUEST_URI} !(.+)\.(gif|css|jpg|jpeg|js|jsp|jsp*|do|do*)$ [NC]

 RewriteRule /wodewiki/(.+)_(.+)_(.+).html  /edmclick.do?senderid=$1&senderIdEn=$2&pici=$3&nexturl=http://xuyuan.qq.com/ele-book.html[PT,NC]

 

 RewriteCond %{REQUEST_URI} !(.+)\.(gif|css|jpg|jpeg|js|jsp|jsp*|do|do*)$ [NC]

 RewriteRule /wodewiki/(.+).html  /edmclick.do?senderid=$1&pici=11&nexturl=http://xuyuan.qq.com/[PT,NC]

 

 RewriteCond %{REQUEST_URI} !(.+)\.(gif|css|jpg|jpeg|js|jsp|jsp*|do|do*)$ [NC]

 RewriteRule /ebook/800.html  /edmclick.do?senderid=800&pici=11&nexturl=http://xuyuan.qq.com/ele-book-maxthon.html[PT,NC]

 

 RewriteCond %{REQUEST_URI} !(.+)\.(gif|css|jpg|jpeg|js|jsp|jsp*|do|do*)$ [NC]

 RewriteRule /ebook/(.+).html  /edmclick.do?senderid=$1&pici=11&nexturl=http://xuyuan.qq.com/elebook.html[PT,NC]

例十

RewriteEngine on

RewriteRule /jiaoshi/index.htmlhttp://jiaoshi.qq.com [R=301,L]

RewriteRule /jiaoshi/ http://jiaoshi.qq.com[R=301,L]

例十、當區分瀏覽器時、前面要求IEMozilla瀏覽器用戶訪問:

http://www.abc.com/ym跳轉到了http://ip/b.html、後面要求非IEMozilla瀏覽器的用戶將跳到http://ip/a.html、匹配方式是從前往後

Apache Rewrite RewriteCond%{HTTP_USER_AGENT} 使用方法

RewriteEngine On

#RewriteCond %{HTTP_USER_AGENT} ^.*MSIE.*

RewriteCond %{HTTP_USER_AGENT}"^Mozilla/4/.0 /(compatible; MSIE.*"

RewriteCond %{HTTP_USER_AGENT}".*Windows.*"

RewriteCond %{HTTP_USER_AGENT}"!.*Windows CE.*"

#RewriteCond %{HTTP_USER_AGENT} !^.*Opera.*

RewriteRule ^/ym(.*)$ http://ip/b.html[R,L,QSA]

 

RewriteEngine On

RewriteRule ^/ym(.*)$ http://ip/a.html[R,L,QSA]

例十二、

#==============================================================#

13g.qq.com域名下添加

<VirtualHost *:80>

 

       RewriteEngine on

       RewriteRule ^/$ /3g/index.jsp [PT,NC]

       RewriteCond %{REQUEST_URI} !(.+)\.(gif|css|jpg|jp2|jpeg|js|jsp|jsp*|do|do*|html|htm)$ [NC]

       RewriteRule ^/(.*)/ /3g/$1.jsp [PT,NC]

 

</VirtualHost>

#操作步驟

cd /application/apache2.2.13/conf/extra/

cp httpd-vhosts.conf httpd-vhosts.conf.xk.`date+%Y%m%d`

vi httpd-vhosts.conf

       #----add-by-xk-2012-02-29---#

       RewriteEngine on

       RewriteRule ^/$ /3g/index.jsp [PT,NC]

       RewriteCond %{REQUEST_URI} !(.+)\.(gif|css|jpg|jp2|jpeg|js|jsp|jsp*|do|do*|html|htm)$ [NC]

        RewriteRule ^/(.*)/ /3g/$1.jsp [PT,NC]

       #---------------------------#

#==============================================================#


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章