apacherewriteRegex基礎

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.