Apache:将多个目录映射到相同的位置

我想用Apache将多个目录映射到相同的位置,期望类似这样的行为:假设/static位置将由directories /srv/static1/srv/static2 。 然后,如果请求/static/image.png ,我希望Apache检查文件/srv/static1/image.png/srv/static2/image.png ,按顺序。 find的第一个文件被返回; 如果两者都不被发现,经典错误404被发回。

当然,解决scheme是使用脚本来创build指向正确位置的符号链接目录,并使用该目录,例如在Alias指令中。 我想知道是否有一个更直接的方式,不需要我部署额外的机器。

谢谢!

这很容易使用mod_rewrite完成。 您可以将其设置为有一个条件,即如果文件不在第一个目录中,则应该在第二个目录中查找,等等。

这是一个简短的示例configuration; 有关更多信息,请参阅规范mod_rewrite问题和Apache的mod_rewrite文档 。

 RewriteEngine on # first try to find it in dir1/... # ...and if found stop and be happy: RewriteCond %{DOCUMENT_ROOT}/dir1/%{REQUEST_URI} -f RewriteRule ^(.+) %{DOCUMENT_ROOT}/dir1/$1 [L] # second try to find it in dir2/... # ...and if found stop and be happy: RewriteCond %{DOCUMENT_ROOT}/dir2/%{REQUEST_URI} -f RewriteRule ^(.+) %{DOCUMENT_ROOT}/dir2/$1 [L] # else go on for other Alias or ScriptAlias directives, # etc. RewriteRule ^ - [PT]