Apache Rewrite:%{HTTP_HOST} 目录中的域特定图库图像

发布于 2025-01-06 00:17:55 字数 1073 浏览 2 评论 0原文

在共享网络托管上,我的软件支持多个域(所有域都指向同一个 public_html 根目录)。每个域都有自己的画廊(在画廊目录中)。由于多个域不能在所有站点的同一目录中拥有自己的资源(例如图像),因此我有与每个 HTTP 主机名匹配的根目录。这是目录结构的样子...

/public_html/

/public_html/.htaccess

/public_html/画廊/

/public_html/www.example1.com/gallery/topic_x/image.png

/public_html/www.example2.com/gallery/topic_y/image.jpg

/public_html/www.example3.com/gallery/topic_z/image.gif

请求...

http://www.example1.com/gallery/topic_x/image.png

...需要重写为...

/public_html/www.example1.com/gallery/topic_x/image.png

这需要使用 .htaccess 文件来完成,如上所述,HTTP 主机匹配目录中没有 .htaccess 文件 (/public_html/www.example1 .com/.htaccess 不存在)。

我一直在尝试对以下内容进行多次修改......

RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(gallery)(/.+\.(gif|jpg|png))$ %{HTTP_HOST}$1$2 [L,NC]

但没有成功。

On shared web-hosting my software supports multiple domains (all domains point to the same public_html root directory). Each domain has it's own gallery (in the gallery directory). Since multiple domains can not have their own resources (e.g. images) in the same directory for all sites I have root directories that match each HTTP host name. Here is what the directory structure looks like...

/public_html/

/public_html/.htaccess

/public_html/gallery/

/public_html/www.example1.com/gallery/topic_x/image.png

/public_html/www.example2.com/gallery/topic_y/image.jpg

/public_html/www.example3.com/gallery/topic_z/image.gif

A request to...

http://www.example1.com/gallery/topic_x/image.png

...needs to be rewritten to...

/public_html/www.example1.com/gallery/topic_x/image.png

This needs to be done using the .htaccess file as is noted above, there are no .htaccess files in the HTTP Host matching directories (/public_html/www.example1.com/.htaccess does not exist).

I have been trying with numerous modifications of the following...

RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(gallery)(/.+\.(gif|jpg|png))$ %{HTTP_HOST}$1$2 [L,NC]

...without success.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

讽刺将军 2025-01-13 00:17:55

这对我有用:

RewriteRule ^(gallery/.+\.(gif|jpg|jpeg|png))$ /%{HTTP_HOST}/$1 [L,NC]

如果你愿意——你可以添加额外的检查来重写,只有当这样的最终图像存在时(虽然我没有测试过这一点):

RewriteCond {%DOCUMENT_ROOT}/%{HTTP_HOST}/$1 -f
RewriteRule ^(gallery/.+\.(gif|jpg|jpeg|png))$ /%{HTTP_HOST}/$1 [L,NC]

但即使你的模式也应该有效(至少它通过了测试)——也许 .htaccess 未启用..或者您忘记激活重写引擎(RewriteEngine On)?

我还建议在激活引擎之前(或之后)添加此行:

Options +FollowSymLinks

Options +SymLinksIfOwnerMatch

某些共享托管可能仅适用于第二个,对于某些第一个就足够了。

This one works for me:

RewriteRule ^(gallery/.+\.(gif|jpg|jpeg|png))$ /%{HTTP_HOST}/$1 [L,NC]

If you want -- you can add extra check to rewrite only if such final image is present (although I have not tested this):

RewriteCond {%DOCUMENT_ROOT}/%{HTTP_HOST}/$1 -f
RewriteRule ^(gallery/.+\.(gif|jpg|jpeg|png))$ /%{HTTP_HOST}/$1 [L,NC]

But even your pattern should work (at least it passes the test) -- maybe .htaccess is not enabled .. or you forgot to activate rewrite engine (RewriteEngine On)?

I would also recommend adding this line before (or after) activating engine:

Options +FollowSymLinks

or

Options +SymLinksIfOwnerMatch

Some shared hosting may only work with 2nd, for some 1st is enough.

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