.htaccess Rewrite 規則詳解

在弄wordPress的固定鏈接時遇到 問題,總是404,最後經過百度、Google,終於找到了問題所在,原來是httpd.conf中的

<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>

紅字部分改成 ALL 就行了,拿出來給大家分享下,另外轉載此博客分享。

1、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。

2、舉例說明:

下面是在一個虛擬主機裏定義的規則。功能是把client請求的主機前綴不是www.colorme.com和203.81.23.202都跳轉到主機前綴爲http://www.colorme.com.cn,避免當用戶在地址欄寫入http://colorme.com.cn時不能以會員方式登錄網站。

NameVirtualHost 192.168.100.8:80

ServerAdmin [email protected]
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 [email protected]
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 [email protected]
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 [email protected]
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地址會改變成新的地址,而使用內部跳轉則瀏覽器中的地址不發生改變,看上去更像實際的二級域名虛擬服務器。

發佈了55 篇原創文章 · 獲贊 23 · 訪問量 74萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章