mod_rewrite - 从特定 HTTP 主机的子文件夹中获取图像

发布于 2025-01-14 06:40:27 字数 424 浏览 3 评论 0原文

我的情况很简单,但我仍然无法使其发挥作用。我的后端控制多个站点,我需要将 /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千寻… 2025-01-21 06:40:28
RewriteCond %{HTTP_HOST} ^www.site2.com
RewriteRule ^img/(.*)$ img/site2/$1 [L]

这将引发 500 错误,因为 RewriteRule 模式 也与目标 URL 匹配,这将导致无限的重写循环。您需要排除已重写为 img/site2/ 的请求,或者可能只匹配 img/,而不是 img/任何东西>

例如,请尝试以下操作:

RewriteCond %{HTTP_HOST} ^www\.(site2)\.com
RewriteRule ^img/([^/]+\.\w{2,4})$ img/%1/$1 [L]

这匹配 /img/logo.svg,但不匹配 /img/site2/logo.svg

%1 反向引用包含从前面的 CondPattern 捕获的域名(即“site2”)。这个简单可以节省重复。

RewriteCond %{HTTP_HOST} ^www.site2.com
RewriteRule ^img/(.*)$ img/site2/$1 [L]

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 to img/site2/, or perhaps just match img/<file>, instead of img/<anything>.

For example, try the following instead:

RewriteCond %{HTTP_HOST} ^www\.(site2)\.com
RewriteRule ^img/([^/]+\.\w{2,4})$ img/%1/$1 [L]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文