Rewrite是一種服務器的重寫脈衝技術,它可使得服務器能夠支持 URL 重寫,是一種最新流行的服務器技術。它還能夠實現限制特定IP訪問網站的功能。php
一、Rewrite標誌html
R[=code](force redirect) 強制外部重定向web
G(force URL to be gone) 強制URL爲GONE,返回410HTTP狀態碼。正則表達式
P(force proxy) 強制使用代理轉發。apache
L(last rule) 代表當前規則是最後一條規則,中止分析之後規則的重寫。api
N(next round) 從新從第一條規則開始運行重寫過程。瀏覽器
C(chained with next rule) 與下一條規則關聯服務器
若是規則匹配則正常處理,該標誌無效,若是不匹配,那麼下面全部關聯的規則都跳過app
T=MIME-type(force MIME type) 強制MIME類型webapp
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) 設置環境變量
二、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)添加環境變量
三、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
四、Rewrite規則表達式的說明:
. 匹配任何單字符
[chars] 匹配字符串:chars
[^chars] 不匹配字符串:chars
text1|text2 可選擇的字符串:text1或text2
? 匹配0到1個字符
* 匹配0到多個字符
+ 匹配1到多個字符
^ 字符串開始標誌
$ 字符串結束標誌
\n 轉義符標誌
反向引用 $N 用於 RewriteRule 中匹配的變量調用(0 <= N <= 9)
反向引用 %N 用於 RewriteCond 中最後一個匹配的變量調用(1 <= N <= 9)
五、實際操做:例子: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>
一、Rewrite規則簡介:
Rewirte主要的功能就是實現URL的跳轉,它的正則表達式是基於Perl語言。可基於服務器級的(httpd.conf)和目錄級的 (.htaccess)兩種方式。若是要想用到rewrite模塊,必須先安裝或加載rewrite模塊。方法有兩種一種是編譯apache的時候就直接 安裝rewrite模塊,別一種是編譯apache時以DSO模式安裝apache,而後再利用源碼和apxs來安裝rewrite模塊。
基於服務器級的(httpd.conf)有兩種方法,一種是在httpd.conf的全局下 直接利用RewriteEngine on來打開rewrite功能;另外一種是在局部裏利用RewriteEngine on來打開rewrite功能,下面將會舉例說明,須要注意的是,必須在每一個virtualhost裏用RewriteEngine on來打開rewrite功能。不然virtualhost裏沒有RewriteEngine on它裏面的規則也不會生效。
基於目錄級的(.htaccess),要注意一點那就是必須打開此目錄的FollowSymLinks屬性且在.htaccess裏要聲明RewriteEngine on。
二、舉例說明:
下面是在一個虛擬主機裏定義的規則。功能是把client請求的主機前綴不是www.colorme.com和203.81.23.202都跳轉到 主機前綴爲http://www.colorme.com.cn,避免當用戶在地址欄寫入http://colorme.com.cn時不能以會員方式登 錄網站。
NameVirtualHost 192.168.100.8:80
ServerAdmin webmaster@colorme.com.cn
DocumentRoot 「/web/webapp」
ServerName www.colorme.com.cn
ServerName colorme.com.cn
RewriteEngine on #打開rewirte功能
RewriteCond %{HTTP_HOST} !^www.colorme.com.cn [NC] #聲明Client請求的主機中前綴不是www.colorme.com.cn,[NC]的意思是忽略大小寫
RewriteCond %{HTTP_HOST} !^203.81.23.202 [NC] #聲明Client請求的主機中前綴不是203.81.23.202,[NC]的意思是忽略大小寫
RewriteCond %{HTTP_HOST} !^$ #聲明Client請求的主機中前綴不爲空,[NC]的意思是忽略大小寫
RewriteRule ^/(.*) http://www.colorme.com.cn/ [L]
#含義是若是Client請求的主機中的前綴符合上述條件,則直接進行跳轉到http://www.colorme.com.cn/,[L]意味着當即停 止重寫操做,並再也不應用其餘重寫規則。這裏的.*是指匹配全部URL中不包含換行字符,()括號的功能是把全部的字符作一個標記,以便於後面的應用.就是 引用前面裏的(.*)字符。
例二.將輸入 folio.test.com 的域名時跳轉到profile.test.com
listen 8080
NameVirtualHost 10.122.89.106:8080
ServerAdmin webmaster@colorme.com.cn
DocumentRoot 「/usr/local/www/apache22/data1/」
ServerName profile.test.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^folio.test.com [NC]
RewriteRule ^/(.*) http://profile.test.com/ [L]
3.Apache mod_rewrite規則重寫的標誌一覽
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) 強制URL爲GONE,返回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 internal sub-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/zoo轉換成/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) 設置環境變量
4.Apache rewrite例子集合
在 httpd 中將一個域名轉發到另外一個域名虛擬主機世界近期更換了域名,新域名爲 www.wbhw.com, 更加簡短好記。這時須要將原來的域名webhosting-world.com, 以及論壇所在地址 webhosting-world.com/forums/定向到新的域名,以便用戶能夠找到,而且使原來的論壇 URL 繼續有效而不出現 404 未找到,好比原來的http://www.webhosting-world.com/forums/-f60.html, 讓它在新的域名下繼續有效,點擊後轉發到http://bbs.wbhw.com/-f60.html, 這就須要用 apache 的 Mod_rewrite 功能來實現。
在中添加下面的重定向規則:
RewriteEngine On
# Redirect webhosting-world.com/forums to bbs.wbhw.com
RewriteCond %{REQUEST_URI} ^/forums/
RewriteRule /forums/(.*) http://bbs.wbhw.com/$1 [R=permanent,L]
# Redirect webhosting-world.com to wbhw.com
RewriteCond %{REQUEST_URI} !^/forums/
RewriteRule /(.*) http://www.wbhw.com/$1 [R=permanent,L]
添加了上面的規則之後, 裏的所有內容以下:
ServerAlias webhosting-world.com
ServerAdmin admin@webhosting-world.com
DocumentRoot /path/to/webhosting-world/root
ServerName www.webhosting-world.com
RewriteEngine On
# Redirect webhosting-world.com/forums to bbs.wbhw.com
RewriteCond %{REQUEST_URI} ^/forums/
RewriteRule /forums/(.*) http://bbs.wbhw.com/$1 [R=permanent,L]
# Redirect webhosting-world.com to wbhw.com
RewriteCond %{REQUEST_URI} !^/forums/
RewriteRule /(.*) http://www.wbhw.com/$1 [R=permanent,L]
URL重定向
例子一:
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]
5.使用Apache的URL Rewrite配置多用戶虛擬服務器
要實現這個功能,首先要在DNS服務器上打開域名的泛域名解析(本身作或者找域名服務商作)。好比,我就把 *.semcase.com和 *.semcase.cn所有解析到了個人這臺Linux Server上。
而後,看一下個人Apache中關於*.semcase.com的虛擬主機的設定。
#*.com,*.osall.net
ServerAdmin webmaster@semcase.com
DocumentRoot /home/www/www.semcase.com
ServerName dns.semcase.com
ServerAlias dns.semcase.com semcase.com semcase.net *.semcase.com *.semcase.net
CustomLog /var/log/httpd/osa/access_log.log」 common
ErrorLog /var/log/httpd/osa/error_log.log」
AllowOverride None
Order deny,allow
#AddDefaultCharset GB2312
RewriteEngine on
RewriteCond %{HTTP_HOST} ^[^.]+\.osall\.(com|net)$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+)\.osall\.(com|net)(.*)$
/home/www/www.semcase.com/sylvan$3?un=$1&%{QUERY_STRING} [L]
在這段設定中,我把*.semcase.net和*.semcase.com 的Document Root都設定到了 /home/www/www.semcase.com
可是,繼續看下去,看到…配置了嗎?在這裏我就配置了URL Rewrite規則。
RewriteEngine on #打開URL Rewrite功能
RewriteCond %{HTTP_HOST} ^[^.]+.osall.(com|net)$ #匹配條件,若是用戶輸入的URL中主機名是相似 xxxx.semcase.com 或者 xxxx.semcase.cn 就執行下面一句
RewriteRule ^(.+) %{HTTP_HOST}$1 [C] #把用戶輸入完整的地址(GET方式的參數除外)做爲參數傳給下一個規則,[C]是Chain串聯下一個規則的意思
RewriteRule ^([^.]+).osall.(com|net)(.*)$ /home/www/dev.semcase.com/sylvan$3?un=$1&%{QUERY_STRING} [L]
# 最關鍵的是這一句,使用證則表達式解析用戶輸入的URL地址,把主機名中的用戶名信息做爲名爲un的參數傳給/home/www /dev.semcase.com目錄下的腳本,並在後面跟上用戶輸入的GET方式的傳入參數。並指明這是最後一條規則([L]規則)。注意,在這一句中 指明的重寫後的地址用的是服務器上的絕對路徑,這是內部跳轉。若是使用http://xxxx這樣的URL格式,則被稱爲外部跳轉。使用外部跳轉的話,瀏 覽着的瀏覽器中的URL地址會改變成新的地址,而使用內部跳轉則瀏覽器中的地址不發生改變,看上去更像實際的二級域名虛擬服務器。
這樣設置後,重啓Apache服務器,測試一下,就大功告成了!