mod_rewrite - 从特定 HTTP 主机的子文件夹中获取图像
我的情况很简单,但我仍然无法使其发挥作用。我的后端控制多个站点,我需要将 /img/ 文件夹简单地重写为特定 HTTP 主机的 /img/subfolder/ 。
因此,从 URL www.site1.com/img/logo.svg
文件将位于 /img/logo.svg
并且从 URL www.site2.com/img/logo.svg
将位于 /img/site2/logo.svg
我在 .htaccess 中有此代码,但它会抛出500 错误
RewriteCond %{HTTP_HOST} ^www.site2.com
RewriteRule ^img/(.*)$ img/site2/$1 [L]
我忽略了什么?谢谢你!
I have very simply situation, but I still can't make it works. My back-end controls more than one site and I need to make simple rewrite of /img/ folder to /img/subfolder/ for specific HTTP hosts.
So, from URL www.site1.com/img/logo.svg
file will be in /img/logo.svg
and from URL www.site2.com/img/logo.svg
will be in /img/site2/logo.svg
I have this code in .htaccess, but it throws 500 error
RewriteCond %{HTTP_HOST} ^www.site2.com
RewriteRule ^img/(.*)$ img/site2/$1 [L]
What am I overlooking? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将引发 500 错误,因为
RewriteRule
模式 也与目标 URL 匹配,这将导致无限的重写循环。您需要排除已重写为img/site2/
的请求,或者可能只匹配img/
,而不是img/
任何东西>
。例如,请尝试以下操作:
这匹配
/img/logo.svg
,但不匹配/img/site2/logo.svg
。%1
反向引用包含从前面的 CondPattern 捕获的域名(即“site2”)。这个简单可以节省重复。This will throw a 500 error because the
RewriteRule
pattern also matches the target URL, which will result in an endless rewrite loop. You need to exclude requests that have already been rewritten toimg/site2/
, or perhaps just matchimg/<file>
, instead ofimg/<anything>
.For example, try the following instead:
This matches
/img/logo.svg
, but not/img/site2/logo.svg
.The
%1
backreference contains the domain name (ie. "site2") captured from the preceding CondPattern. This simple saves repetition.