2014-10-07 27 views
0

是的,另一個的RewriteCond,但我拉我的頭髮試圖得到它的工作....用於圖像的.htaccess的RewriteCond在另一個文件夾

我有Apache的根爲/var/www/html/,並且如果我去http://example.com/它正確使用/var/www/html/index.php

現在,問題是我有/var/www/images/example.com/中的圖像。這是因爲我有一個需要動態的多站點設置。 PHP會整理使用哪個網站的代碼,因此移動圖片或對其進行別名不是一個選項,因爲它需要動態。

所以,我在我的/var/www/html/.htaccess是這樣的:

Options -MultiViews -Indexes 

RewriteEngine on 

RewriteCond /var/www/images/example.com%{REQUEST_URI} -f 
RewriteRule ^(.+)$ /var/www/images/example.com/$1 # <--- this DOES fire when the image exists 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /var/www/html/test.php?file=%{REQUEST_FILENAME} [END] 

test.php只是回聲$_GET,而奇怪的是,$_GET['file']/var/www/html/var

我在做什麼錯?


更新

好了,現在我有這個:

RewriteCond %{REQUEST_URI} !^/sites/ 
RewriteRule ^(.*)$ sites/%{HTTP_HOST}/$1 [NC] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule . index.php 

我改變它,這樣的網站文件是主服務器的根目錄裏面。現在我不能讓它不允許直接訪問這些文件。

我想是這樣的:

http://example.com/images/fb.png - > /var/www/sites/example.com/images/fb.png http://example.com/sites/example.com/images/fb.png - > /var/www/index.php(顯示404) http://example.com/whatever - > /var/www/index.php

正如你所看到的,第二行的文件是該文件的全路徑直接和應該通關到index.php,而是它只是發送文件。如何防止這種情況?

回答

1

你不能重寫那些不在你的文檔根目錄中的東西(出於安全原因,否則人們只會注入重寫到/etc/passwd)。這意味着你不能對任何不在/var/www/html/中的東西做任何事情。你需要做的是使用一個腳本來訪問文檔根目錄之外的文件。也許是這樣的:

RewriteCond /var/www/images/example.com%{REQUEST_URI} -f 
RewriteRule ^(.+)$ /image_serve.php?img=example.com/$1 [L] 

,然後將image_serve.php腳本會做這樣的事情:

<?php 
    if (substr($_GET['img'], -strlen('.png')) === '.png') 
    header("Content-Type: image/png"); 
    else if (substr($_GET['img'], -strlen('.gif')) === '.gif') 
    header("Content-Type: image/gif"); 
    else if (substr($_GET['img'], -strlen('.jpg')) === '.jpg') 
    header("Content-Type: image/jpeg"); 

    readfile('/var/www/images/' . $_GET['img']); 
?> 
+0

好吧,解釋它。 – Sarke 2014-10-07 03:12:10

+0

但是,我不想爲每個圖像使用PHP,因爲使用APACHE加載它們要快得多。我會解決一些問題,謝謝。 – Sarke 2014-10-07 03:12:53

+0

即使在將圖像移動到'/ var/www/html /'後,我仍然會遇到同樣奇怪的重定向問題。 – Sarke 2014-10-07 03:27:56

0

此我想要做什麼。

# rewrite to site subfolder 
RewriteRule ^(.*)$ sites/%{HTTP_HOST}/$1 [NC,QSD,DPI] 

# if file doesn't exists in subfolder, just pass it to index.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule . index.php [END] 

# prevent another go-around 
RewriteRule .* - [END] 
相關問題