ServerAdmin [email protected]
DocumentRoot “/web”
ServerName kiya.cn
RewriteCond %{HTTP_HOST} !^www.kiya.cn [NC] #聲明Client請求的主機中前綴不是www.kiya.cn ,其中 [NC] 的意思是忽略大小寫
RewriteCond %{HTTP_HOST} !^70.40.213.183 [NC] #聲明Client請求的主機中前綴不是70.40.213.183,其中 [NC] 的意思是忽略大小寫
RewriteCond %{HTTP_HOST} !^$ #聲明Client請求的主機中前綴不爲空
RewriteRule ^(.*) http://www.kiya.cn/ [L] #含義是如果Client請求的主機中的前綴符合上述條件,則直接進行跳轉到http://www.kiya.cn/,[L ]意味着立即停止重寫操作,並不再應用其他重寫規則。這裏的.*是指匹配所有URL中不包含換行字符,()括號的功能是把所有的字符做一個標記,以便於後面的應用.就是引用前面裏的(.*)字符。
RewriteCond %{HTTP_HOST} ^en.sicasoft.com [NC]
RewriteRule ^(.*) http://www.sicasoft.com/ [L]
按照這樣的要求重定向規則應該這樣寫:
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]
強制在替代字符串加上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) 與下一條規則關聯
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=zoo
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) 設置環境變量
同時達到下面兩個要求:
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 的功能
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=*&page=* –> /type*page*.html
RewriteRule ^/type([0-9]+)page([0-9]+).html$ /type.php?typeid=$1&page=$2 [PT]
DocumentRoot /home/www/www.kiya.us
ServerName dns.kiya.us
ServerAlias dns.kiya.us kiya.us *.kiya.us
CustomLog /var/log/httpd/osa/access_log.log” common
ErrorLog /var/log/httpd/osa/error_log.log”
AllowOverride None
Order deny,allow
RewriteCond %{HTTP_HOST} ^[^.]+.kiya.(cn|us)$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+).kiya.(cn|us)(.*)$ /home/www/www.kiya.us/sylvan$3?un=$1&%{QUERY_STRING} [L]
RewriteCond %{HTTP_HOST} ^[^.]+.kiya.(cn|us)$ #匹配條件,如果用戶輸入的URL中主機名是類似 xxxx.kiya.us 或者 xxxx.kiya.cn 就執行下面一句
RewriteRule ^(.+) %{HTTP_HOST}$1 [C] #把用戶輸入完整的地址(GET方式的參數除外)作爲參數傳給下一個規則,[C]是Chain串聯下一個規則的意思
RewriteRule ^([^.]+).kiya.(cn|us)(.*)$ /home/www/dev.kiya.us/sylvan$3?un=$1&%{QUERY_STRING} [L]
# 最關鍵的是這一句,使用證則表達式解析用戶輸入的URL地址,把主機名中的用戶名信息作爲名爲un的參數傳給/home/www/dev.kiya.us 目錄下的腳本,並在後面跟上用戶輸入的GET方式的傳入參數。並指明這是最後一條規則([L]規則)。注意,在這一句中指明的重寫後的地址用的是服務器上 的絕對路徑,這是內部跳轉。如果使用http://xxxx 這樣的URL格式,則被稱爲外部跳轉。使用外部跳轉的話,瀏覽着的瀏覽器中的URL地址會改變成新的地址,而使用內部跳轉則瀏覽器中的地址不發生改變,看上去更像實際的二級域名虛擬服務器。
RewriteEngine On
RewriteCond %{HTTP_REFERER} chinaz.com [NC]
RewriteCond %{HTTP_REFERER} im286.com [NC]
RewriteRule .*/.(jpg|jpeg|gif|png|rar|zip|txt|ace|torrent|gz|swf)$ http://www.xxx.com/fuck.png [R,NC,L]
答案是,兩者都是可以的。
在HostMonster以及我自己的機器上,是用
RewriteRule ^business/([^/.]+)$ biz/detail.php?name=$1 [L]
達到改寫的。而在Godaddy主機上,是這樣:
RewriteRule ^business/([^/.]+)$ /biz/detail.php?name=$1 [L]
目標文件前多了一個/
現在想想,可能是因爲沒有指定RewriteBase,至於到底是不是我改日再驗證一下。
1
RewriteCond %{HTTP_USER_AGENT} ^MSIE [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Opera [NC]
RewriteRule ^.* – [F,L] 這裏”-”表示沒有替換,瀏覽器爲IE和Opera的訪客將被禁止訪問。
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>
RewriteRule (.*.css$|.*.js$) gzip.php?$1 [L]
如果要排除一些擴展名:
RewriteRule !/.(js|ico|gif|jpg|JPG|png|PNG|css|pdf|swf)$ index.php