Apache 重写特定文件请求
我正在尝试将 Apache 设置为在虚拟主机下请求时重写特定文件名。目标是根据所使用的主机标头更改某些品牌元素(所有主机标头都具有虚拟主机配置)。
我按照下面的示例在 nginx 中进行了此操作
if ($host = "test.example.com") {
rewrite ^/images/file1.png$ /images/otherfile1.png;
rewrite ^/images/file2.png$ /images/differentfile2.png;
break;
}
对于 Apache,我在虚拟主机文件中使用了以下配置,并确认该模块正在运行,但它似乎不起作用(使用了原始 file1)。
<Directory "/opt/site/html/">
AllowOverride All
Options FollowSymLinks MultiViews
Require all granted
RewriteEngine On
RewriteBase /
RewriteRule ^/images/file1.png$ /images/otherfile1.png
RewriteRule ^/images/file2.png$ /images/differnetfile2.png
</Directory>
任何关于我在启用此重写时所缺少的内容的建议将不胜感激。
I'm trying to setup Apache to rewrite particular file names when requested under a virtual host. The goal is to change certain branding elements based on the host header used (all of which have virtual host configs).
I had this working in nginx as per the example below
if ($host = "test.example.com") {
rewrite ^/images/file1.png$ /images/otherfile1.png;
rewrite ^/images/file2.png$ /images/differentfile2.png;
break;
}
For Apache I used the following config in the virtual hosts file, and confirmed the module is running, but it does not seem work (the original file1 is used).
<Directory "/opt/site/html/">
AllowOverride All
Options FollowSymLinks MultiViews
Require all granted
RewriteEngine On
RewriteBase /
RewriteRule ^/images/file1.png$ /images/otherfile1.png
RewriteRule ^/images/file2.png$ /images/differnetfile2.png
</Directory>
Any advice on what I'm missing to enable this rewrite would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些指令位于
容器内(
内)。在目录上下文中(与.htaccess
一样),RewriteRule
模式匹配的URL路径不匹配以斜线开头。所以上面的规则不会匹配。指令应该像这样:注意:不要忘记在正则表达式中反斜杠转义文字点,并且您应该在此处使用
L
标志来停止重写引擎的进一步处理。或者,您可以将这些规则从
包装器中移出并直接移入
容器中。在这种情况下,规则与完整的根相对 URL 路径匹配,以斜线开头(如您所写)。但在虚拟主机上下文中,不允许使用RewriteBase
指令,因此必须将其删除(无论如何您都不会使用它)。旁白:
您允许 .htaccess 覆盖(第一个指令)。请注意,如果您确实有一个包含 mod_rewrite 指令(< 中的指令。 /code> 服务器配置中的容器。您可能想要显式禁用
RewriteRule
等)的.htaccess
文件,那么这些文件将完全覆盖相应.htaccess
覆盖,例如:您还启用
MultiViews
- 这是故意的吗?这基本上启用了开箱即用的无扩展 URL(在所有内容上),并且可能与 mod_rewrite 发生冲突,因此通常应该禁用。只需将其从上述规则中删除即可禁用此功能,例如:您实际上并没有在发布的指令中使用它。
These directives are inside a
<Directory>
container (inside a<VirtualHost>
). In a directory context (as with.htaccess
), the URL-path matched by theRewriteRule
pattern does not start with a slash. So the above rules will not match. The directives should be like this instead:NB: Don't forget to backslash-escape literal dots in the regex and you should use the
L
flag here to stop further processing by the rewrite engine.Alternatively, you can move these rules out of the
<Directory>
wrapper and into the<VirtualHost>
container directly. In this context, the rules match against the full root-relative URL-path, starting with a slash (as you have written). But in a virtualhost context, theRewriteBase
directive is not permitted so must be removed (you are not making use of this anyway).Aside:
You are allowing
.htaccess
overrides (first directive). Note that if you do have an.htaccess
file with mod_rewrite directives (RewriteRule
etc.) then these will completely override the directives in the corresponding<Directory>
container in the server config. You may want to explicitly disable.htaccess
overrides, for example:You've also enabled
MultiViews
- is that intentional? This basically enables extensionless URLs out-of-the-box (on everything) and can potentially conflict with mod_rewrite, so generally, this should be disabled. Simply remove it from the above rule to disable this, for example:You're not actually using this in the directives you've posted.