[APACHE] 關於RewriteCond $1

URL Link //n.sfs.tw/11099

2017-05-17 09:10:17 By 張○○

在閱讀 codeIgniter 時,發現他們的rewrite 寫法[2],雖然知道作用,但不知道原理,直到看到[1]的說明才了解,筆記下來。

RewriteEngine on

RewriteBase /

RewriteCond $1 !^(index\.php|images|robots\.txt|$)

RewriteRule ^(.*)$ index.php/$1 [L,QSA]

[1]. 符合此regex,進行[2]
[2]. [1]中刮弧中內容,變成[2]的$1進行[3]的判斷是否符合
[3]. 判斷不符合,比對結束;判斷符合非 index.php或 image或rebots.txt 則進行[4]
[4]. 改寫 index.php/$1 L代表比對結束,QSA代表附上後面參數

終於懂了,再看 symfony (PHP framework) 的寫法

 RewriteCond %{REQUEST_FILENAME} !-f

 RewriteRule ^(.*)$ app.php [QSA,L]

和 Slim (PHP framework)中第一行只要檔案不存在,就改寫成 index.php

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^ index.php [QSA,L]

能幫助理解

參考資料

[1] RewriteCond $1 http://www.webmasterworld.com/apache/4330396.htm

[2] codeIgniter 說明 http://www.codeigniter.org.tw/user_guide/general/urls.html


原文 2013-09-02 01:22:36