2013-10-03 22 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