2

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

路径/其他路径/my-image.jpg

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

我可以在 .htaccess 中做什么来将给定的文件扩展名重定向到目录?

即我的图像 src 只是“my-image.jpg”,.htaccess 看到这是一个 .jpg 文件并重定向到“my-image.jpg”所在的文件夹“www.site.com/my-jpgs/”,从而加载图像。

这是我目前所拥有的:

    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

第二个是正确的,我该如何更正它才不会提出第一个不正确的请求?

4

1 回答 1

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 toindex.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]
于 2013-10-03T16:41:27.227 回答