Apache的重写:映像目录基于HTTP主机上 [英] Apache Rewrite: image directory based on HTTP host

查看:191
本文介绍了Apache的重写:映像目录基于HTTP主机上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的软件支持多个域名都指向了服务器上的同一目录下(不同的数据库,每门课程)。因此,这些领域...

My software supports multiple domain names all pointed at the same directory on the server (a different database for each of course). So these domains...

www.example1.com
www.example2.com
www.example3.com

......都指向...

...all point to...

/public_html/

在图像目录...

/public_html/images/

我有一个主机名完全匹配,每个网站的目录...

I have directories that exactly match the host names for each website...

/public_html/images/www.example1.com/
/public_html/images/www.example2.com/
/public_html/images/www.example3.com/

我试图让阿帕奇重写请求,因此,如果您直接查看图像,并期待在地址栏只能看到主机名一次。

I'm trying to get Apache to rewrite requests so that if you view the image directly and look at the address bar you only see the host name once.

因此​​,对于...请求

So a request for...

http://www.example1.com/images/book.png

...由阿帕奇在...进账

...is fetched by Apache at...

/public_html/images/www.example1.com/book.png

一个我试过,并在不同的情况下取得了成功的事情是下面的,虽然它并没有在这种情况下工作...

One of the things I've tried and have had success with in different circumstances is the following though it doesn't work in this situation...

RewriteRule ^[^/]*/images(.+) images/%{HTTP_HOST}/$1

将竭诚为作出任何澄清,以及两个了票,并接受了答案,如果有人可以请帮助我得到这个工作。

Will be happy to make any clarifications as well as both up-vote and accept the answer if someone can please help me get this working.

推荐答案

尝试添加以下到您的网站(的public_html)的根目录下的的.htaccess 文件

Try adding the following to the .htaccess file in the root directory of your site (public_html)

RewriteEngine on
RewriteBase / 

#prevent looping from internal redirects
RewriteCond %{ENV:REDIRECT_STATUS} !200
#only rewrite gif, jpg or png
RewriteRule ^(images)(/.+\.(gif|jpg|png))$ $1/%{HTTP_HOST}$2 [L,NC] 

您的规则

RewriteRule ^[^/]*/images(.+) images/%{HTTP_HOST}/$1

没有工作,因为你有一个领先的 / 图片。在.htaccess中的领先 / 被删除,所以该规则永远不会匹配。

did not work because you have a leading / before images. In .htaccess the leading / is removed, so the rule would never match.

这篇关于Apache的重写:映像目录基于HTTP主机上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆