2013-10-03 19 views
2

在我的應用程序,我有很多圖片的這樣的SRCS:重定向文件類型的具體要求

路徑/其他路徑/我-image.jpg的

這可以長期做下去,可以顯露出一些我的文件結構到外面的世界。

我能在.htaccess中將給定的文件擴展名重定向到一個目錄嗎?

即我的圖像src只是「my-image.jpg」,.htaccess看到這是一個.jpg文件並重定向到文件夾「www.site.com/my-jpgs/」是「我的圖像。 jpg「駐留,從而加載圖像。

這裏是我的時刻:

Options +FollowSymLinks 
IndexIgnore */* 
RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !^/importers 
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$ 

RewriteRule ^(.*)$ public_html/index.php 

RewriteBase/

# Ignore rule if the real path exists, use that instead. 
RewriteCond %{REQUEST_FILENAME} !-f 
# Only use the filename and extension. 
RewriteRule (.[^/]*)\.(gif|jpg|png) public_html/images$1.$2 [R,L] 

我有點懂了工作,除了瀏覽器,使看起來像這樣的兩個請求:

請求URL: http://www.view.com/activity/enquiry/dropdown-btn.gif

請求URL:http://www.view.com/public_html/images/dropdown-btn.gif

第二個是正確的,我該如何糾正這個問題,以便它不會發出第一個錯誤的請求?

回答

0

的重寫規則排序爲.htaccess. Your problem is incorrect ordering of your rule. You need to move last rule to the top just below RewriteEngine敘述非常重要的在line to have external redirect before sending everything off to index.php`:

Options +FollowSymLinks 
IndexIgnore */* 
RewriteEngine On 
RewriteBase/

# Ignore rule if the real path exists, use that instead. 
RewriteCond %{REQUEST_FILENAME} !-f 
# Only use the filename and extension. 
RewriteRule ^(?:[^/]+/)*([^.]+\.(?:gif|jpe?g|png))$ images/$1 [R,L,NC] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !^/importers 
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png)$ 
RewriteRule^index.php [L] 
+0

謝謝,但它仍然是要求它的兩倍。 – imperium2335

+0

你在瀏覽器中輸入了什麼網址? – anubhava

+0

我在的網址是ht tp://www.site.com/activity/enquiry/50647 GIF請求正在我的編輯中。 – imperium2335