重定向文件类型特定请求

时间:2013-10-03 11:06:46

标签: .htaccess mod-rewrite redirect

在我的应用中,我有许多像这样的图像:

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

这可能会变长,并且可能会将我的一些文件结构暴露给外界。

如何在.htaccess中将给定的文件扩展名重定向到目录?

即。我的图像src只是“my-image.jpg”,。htaccess看到这是一个.jpg文件并重定向到文件夹“www.site.com/my-jpgs/”是“my-image.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]

我有点工作,除了浏览器发出两个看起来像这样的请求:

请求网址:http://www.view.com/activity/enquiry/dropdown-btn.gif

请求网址:http://www.view.com/public_html/images/dropdown-btn.gif

第二个是正确的,我该怎么纠正这个,所以它不会发出第一个错误的请求?

1 个答案:

答案 0 :(得分:0)

重写规则的排序在.htaccess. Your problem is incorrect ordering of your rule. You need to move last rule to the top just below RewriteEngine On 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]