apacherewriteRegex基礎
用了好幾次rewrite,用的次數不是很多,每次都忘,都得重新上網上找一堆現看,總結一下,以免以後忘了
=====================分隔字元=====================
注意以下幾點:
1、載入rewrite模組,LoadModule rewrite_module modules/mod_rewrite.so
2、如果是用vhost裡配置,則在vhost裡開啟RewriteEngine on(htaccess檔案配置的方式沒有用過)
用過的配置:
情境一:當網域名稱是aaa.bbb的時候,跳轉到aaa.ccc.bbb這個網域名稱
RewriteEngine on
RewriteCond %{HTTP_HOST} ^aaa\.bbb$ [NC]
RewriteRule ^/(.*)$ http://aaa.ccc.bbb/$1 [R=301,L,NC]
%{HTTP_HOST}匹配網域名稱,^表示匹配開始,$表示匹配結束,分別是匹配字串的左右界,可以不同時出現,\.表示轉義,只匹配.,否則就變成任意單個字元了
NC(no case) 不區分大小寫,
.是任意單個字元,.*就是任意數量的任一字元,()是子字串,所以^/(.*)$表示網域名稱後任意路徑,這裡猜測,應該可以省略$,這裡的.,沒有用\轉義,是因為這部分不是正則,$1匹配的是第一個()裡的內容,R=301表示永久跳轉,
情境二:把/testt/這個請求,轉到/image/下,但是/testt/xxxx,不發生跳轉
RewriteEngine on
RewriteRule ^/testt/$ http://localhost:8000/image/ [R=301,L,NC]
對於query string的操作,以下引自官方文檔
Modifying the Query String
By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.
=====================分隔字元=====================
參考資料:
Regex中各種字元的含義 http://www.cnblogs.com/afarmer/archive/2011/08/29/2158860.html
apache的rewrite規則使用說明 http://www.jb51.net/article/48780.htm
13個實用的Apache Rewrite重寫規則 http://www.jb51.net/article/47907.htm