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

Gio*_*ani 1 configuration static-files apache-2.2

我想使用 Apache 将多个目录映射到同一位置,期待与此类似的行为:假设该/static位置由目录/srv/static1/srv/static2. 然后,如果/static/image.png被要求,我希望 Apache 以该顺序检查文件/srv/static1/image.png/srv/static2/image.png。返回找到的第一个文件;如果两者都未找到,则发回经典错误 404。

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

谢谢!

Jen*_*y D 6

这可以使用 mod_rewrite 轻松完成。您可以设置一个条件,如果文件不存在于第一个目录中,则它应该在第二个目录中查找,依此类推。

这是一个简短的示例配置;有关更多信息,请参阅规范的 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] 
Run Code Online (Sandbox Code Playgroud)