Apache Rewrite url重新導向功能的簡單配置_Linux

來源:互聯網
上載者:User
1.Apache Rewrite的主要功能
就是實現URL的跳轉和隱藏真真實位址,基於Perl語言的Regex規範。平時協助我們實現擬靜態,擬目錄,網域名稱跳轉,防止盜鏈等

2.Apache Rewrite的配置
Apache下的Rewrite配置主要有兩種,一種是針對整個apache伺服器的配置,此種配置的Rewrite規則是直接在httpd.conf下書寫。配置步驟如下:
(1)去除httpd.conf檔案中"#LoadModule rewrite_module modules/mod_rewrite.so"前面的"#"號;
(2)然後再在httpd.conf中書寫如下規則:
RewriteEngine on
#當訪問任何以t_開頭,以.html結尾的檔案時,將$1用與(.*)匹配的字元替換後,訪問相應的test.php頁面
RewriteRule ^/t_(.*).html$ /test.php?id=$1

另一種是針對apache伺服器下的某一目錄的配置,此種配置的Rewrite規則需在此目錄下建立一個.htaccess檔案來書寫。配置步驟如下:
(1)去除httpd.conf檔案中"#LoadModule rewrite_module modules/mod_rewrite.so"前面的"#"號;
(2)修改httpd.conf檔案中的"AllowOverride None"為"AllowOverride all",同時最好將Options也置為"all",否則可能會出問題。
(3)在目錄中建立.htaccess檔案,並用記事本開啟,書寫如下規則:
RewriteEngine on
RewriteRule ^/t_(.*).html$ /test.php?id=$1

3.Apache Rewrite規則的書寫
RewriteEngine on
RewriteRule ^/test([0-9]*).html$ /test.php?id=$1
RewriteRule ^/new([0-9]*)/$ /new.php?id=$1 [R]

RewriteEngine on
#當我們訪問的地址不是以www.163.com開頭的,那麼執行下一條規則
RewriteCond %{HTTP_HOST} !^www.163.com [NC]
RewriteRule ^/(.*) http://www.163.com/ [L]

4.Apache Rewrite規則修正符
1) R 強制外部重新導向
2) F 禁用URL,返回403HTTP狀態代碼。
3) G 強制URL為GONE,返回410HTTP狀態代碼。
4) P 強制使用代理轉寄。
5) L 表明當前規則是最後一條規則,停止分析以後規則的重寫。
6) N 重新從第一條規則開始運行重寫過程。
7) C 與下一條規則關聯

如果規則匹配則正常處理,以下修正符無效

8) T=MIME-type(force MIME type) 強制MIME類型
9) NS 只用於不是內部子請求
10) NC 不區分大小寫
11) QSA 追加請求字串
12) NE 不在輸出轉義特殊字元 \%3d$1 等價於 =$1

聯繫我們

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