.htaccess htaccess无限重定向

.htaccess htaccess无限重定向,.htaccess,redirect,.htaccess,Redirect,我正在开发一个遗留代码库,其中包含三个不同的图像文件夹。一个图像可以以非常随机的方式存在于三个文件夹中的任意一个文件夹中 我最初试图检查PHP中是否存在文件;然而,图像被显示在很多地方,我希望我能用htaccess文件实现同样的效果。然而,到目前为止没有运气 以下是到目前为止我得到的信息: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^F

我正在开发一个遗留代码库,其中包含三个不同的图像文件夹。一个图像可以以非常随机的方式存在于三个文件夹中的任意一个文件夹中

我最初试图检查PHP中是否存在文件;然而,图像被显示在很多地方,我希望我能用htaccess文件实现同样的效果。然而,到目前为止没有运气

以下是到目前为止我得到的信息:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^Folder_1/(.*)$   Folder_2/$1   [L,NC]
RewriteRule ^Folder_2/(.*)$   Folder_3/$1   [L,NC]
我想做的是,对于任何不存在的图像请求,检查其他两个文件夹,以任何顺序,是否存在。因此,如果有人请求文件夹_3/image.png,它将检查文件夹_1/image.png,然后检查文件夹_2/image.png


关于如何编写htaccess文件有什么建议吗?

使用
[N]
标志再次从顶部开始重写。

如果您无法使其工作,我建议这样做,尽管有点复杂,但似乎可以工作:

RewriteEngine on

# If the request points to a real resource, just end here
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]

# Check the other two directories
# First, figure out which directory we're currently in and capture the rest
RewriteCond $1|Folder_1|Folder_2|Folder_3 ^([^\|]+)\|(.*?)\|?\1\|?(.*)$
# Now break them up into two folders
RewriteCond %2|%3 ([^\|]+)\|([^\|]+)
# Check the first alternate folder to see if it exists there
RewriteCond %{DOCUMENT_ROOT}/%1/$2 -f [OR]
# If not, make the second option move to the first back reference
RewriteCond %2 ^(.*)$
# Check the second alternate folder to see if it exists there
# (or check the first option again if it succeeded..we could condition
#  this out in that case, but I don't know if it's worth adding)
RewriteCond %{DOCUMENT_ROOT}/%1/$2 -f
# If so rewrite to the correct alternate folder
RewriteRule ^([^/]+)/(.*)$ /%1/$2
A只属于下一个。这意味着您需要将这些
RewriteCond
指令放在每个
RewriteRule
的前面:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Folder_1/(.+) Folder_2/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Folder_2/(.+) Folder_3/$1 [L,NC]
顺便说一下:您的第二个条件
%{REQUEST\u FILENAME}-d
可能没有用,因为它只测试现有目录