Apache的Rewrite規則學習

來源:互聯網
上載者:User

標籤:des   style   http   io   color   ar   os   使用   sp   

    1. 1、Rewrite規則簡介: 
      Rewirte主要的功能就是實現URL的跳轉,它的Regex是基於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.字元含義介紹:

      Rewrite標誌

      R[=code](force redirect) 強制外部重新導向

      G(force URL to be gone) 強制URL為GONE,返回410HTTP狀態代碼。

      P(force proxy) 強制使用代理轉寄。

      L(last rule) 表明當前規則是最後一條規則,停止分析以後規則的重寫。

      N(next round) 重新從第一條規則開始運行重寫過程。

      C(chained with next rule) 與下一條規則關聯

      如果規則匹配則正常處理,該標誌無效,如果不匹配,那麼下面所有關聯的規則都跳過

      T=MIME-type(force MIME type) 強制MIME類型

      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) 設定環境變數

    1. 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

  1. 3.實際操作:

    例子: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>

     

    在httpd-vhosts.conf中的寫法如下:

    RewriteRule ^/world/([\d]+)_([A-Z]+).html$   /world/index.php?c=world&a=houseDetail&houseId={$1}&housetype={$2} [L]

    RewriteRule ^/pinggu/([a-z]+)/([0-9]+).html /pinggu/index.php?c=pinggu&a=detail&city=$1&newcode=$2&rewrite=1 [NC,NE]

    RewriteRule ^/jiaju/([a-z]+)/jc_c([\d]+).html /jiaju/index.php?c=jiaju&a=buildList&city=$1&cid=$2

    RewriteRule ^/esf_bs/([a-z]*)_?(\d*)_?b?(\d*)/?m?([0-9,]*)x?(\d*)a?([0-9,]*)c?(\d*)p?([0-9,]*)d?(\d*)/$ /esf/index.php?purpose=%B1%F0%CAFB&city=$1&district=$2&comarea=$3&price=$4&orderby=$5&area=$6&tags=$7&buildclass=$8&equipment=$9&rewrite

     

    RewriteRule ^/world/([\d]+)_([A-Z]+).html$ /world/index.php?c=world&a=houseDetail&houseId={$1}&housetype={$2} [L]

     

    #住宅rewrite規則
      RewriteRule^/zf/([a-z]*)_?(\d*)_?(\d*)/?p?(\d*)h?(\d*)m?(\d*)c?(\d*)([a-z0-9]*)/$   $8&&purpose=&city=$1&district=$2&comarea=$3&price=$4&room=$5&rtype=$6&tags=$7  [C,NC]
      RewriteRule^s?(\d*)r?(\d*)o?(\d*)f?(\d*)d?(\d*)p?x?(\d*)&&(.*) /zf/index.php?$7&housetype=$1&railway=$2&towards=$3&floor=$4&equipment=$5&orderby=$6&rewrite [QSA,L,NC]
      
    #別墅rewrite規則
      RewriteRule ^/zf_bs/([a-z]*)_?(\d*)_?(\d*)/?p?(\d*)t?(\d*)m?(\d*)s?(\d*)([a-z0-9]*)/?$ $8&&purpose=%B1%F0%CA%FB&city=$1&district=$2&comarea=$3&price=$4&buildclass=$5&rtype=$6&housetype=$7 [C,NC]
      RewriteRule ^h?(\d*)o?(\d*)f?(\d*)d?(\d*)p?x?(\d*)&&(.*) /zf/index.php?$6&room=$1&towards=$2&floor=$3&equipment=$4&orderby=$5&rewrite [QSA,L,NC]
      
    #寫字樓rewrite規則
      RewriteRule ^/zf_app_xzl/([a-z]*)_?(\d*)_?(\d*)/?p?(\d*)a?(\d*)t?(\d*)c?(\d*)r?(\d*)/? /zf/index.php?purpose=%D0%B4%D7%D6%C2%A5&app_type=app&city=$1&district=$2&comarea=$3&price=$4&area=$5&propertysubtype=$6&tags=$7&&railway=$8&rewrite [QSA,L]
      
    #寫字樓rewrite規則
     RewriteRule ^/zf_xzl/([a-z]*)_?(\d*)_?(\d*)/?p?(\d*)a?(\d*)t?(\d*)c?(\d*)r?(\d*)/? /zf/index.php?purpose=%D0%B4%D7%D6%C2%A5&city=$1&district=$2&comarea=$3&price=$4&area=$5&propertysubtype=$6&tags=$7&&railway=$8&rewrite [QSA,L]
     
    #寫字樓rewrite規則
       RewriteRule ^/zf_xzl/([a-z]*)_?(\d*)_?(\d*)/?p?(\d*)a?(\d*)t?(\d*)c?(\d*)r?(\d*)/? /zf/index.php?purpose=%D0%B4%D7%D6%C2%A5&city=$1&district=$2&comarea=$3&price=$4&area=$5&propertysubtype=$6&tags=$7&&railway=$8&rewrite [QSA,L]   
       RewriteRule ^/zf/([a-z]+)_xm([0-9]+)/$ /zf/index.php?c=zf&a=index&city=$1&projcodes=$2&src=xiaoqu&rewrite [L]

     

     

Apache的Rewrite規則學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.