2009-07-05 109 views
1

我正在嘗試使用.htaccess文件和Apache創建條件重寫規則。.htaccess有條件重寫

基本上我需要的是Apache會檢查所請求的文件是否存在於一個子文件夾中,Apache將如何處理這個存在的文件,或者如果文件不存在,Apache會加載index.php文件。

我正在使用的文件夾結構是:

- /gallery 
    - /cache 
    - index.php 
    - .htaccess 

也和.htaccess我現在已經是

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule ^(.+) - [PT,L] 
RewriteRule ^(.*)$ index.php/$1 [L] 
然而

,這隻有當我試圖尋找文件因爲在/ gallery文件夾中,但是我需要查看文件是否存在於/ cache文件夾中。

有誰知道如何修改此規則,以實現這一目標?

謝謝你在前進,

Titel的

+0

我不認爲mod_rewrite可以做到這一點,但我有興趣看看是否有可能。 – MitMaro 2009-07-05 05:11:56

回答

1

嘗試下列規則:

RewriteCond %{REQUEST_URI} ^/(([^/]+/)*)gallery/ 
RewriteCond %{DOCUMENT_ROOT}%1gallery/cache/$0 -f 
RewriteRule ^.+ cache/$0 [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^.* index.php/$0 [L] 

第一條規則校驗所請求的路徑是路徑到現有文件中緩存目錄。如果是這樣,它會將該請求重寫爲該文件。

+0

首先讓我感謝你爲此嘗試提供幫助。 但是,您提供的腳本似乎不適用於我的設置。 當我試圖訪問像「example.com/gallery/220-165-clouds.jpg」這樣的URL時,即使圖像存在於/ cache文件夾中,我也不會獲取圖像,而且我也不會也不會得到index.php文件。 我得到的是一個404錯誤:( 你知道什麼可能是這樣做的原因 – titel 2009-07-05 15:00:46