htaccess照片从主域到其他域

时间:2013-11-20 13:39:49

标签: php regex apache .htaccess mod-rewrite

我在domainA.co.uk上有照片,我也需要在domainB.co.uk和domainC.com上使用,但是我只想存储一次照片,但在其他域名上引用它们但相对于每个域

是否有办法在domainB和domainC上使用htaccess来屏蔽照片网址,就好像它们位于每个网址上一样。

尝试使用以下内容但重定向到domainA上的照片:

RewriteRule ^photos/([0-9]{1,15})-([A-Za-z-0-9_]{1,100}).(jpg|png)$ http://files.domainA.co.uk/location-photos/$2_$1.$3 [NC,L]

1 个答案:

答案 0 :(得分:2)

如果不使用proxy,则无法做到这一点。

您需要在Apache配置中为domainBdomainC启用mod_proxy。启用mod_proxy后,通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT中:

RewriteEngine On

RewriteRule ^photos/([0-9]{1,15})-([A-Za-z-0-9_]{1,100}).(jpg|png)$ http://files.domainA.co.uk/location-photos/$2_$1.$3 [NC,L,P]

更新:根据评论,请尝试使用此规则限制某些域的此规则:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^hh\.co\.uk$ [NC]
RewriteRule ^photos/destinations/(.+?\.(?:jpg|png))$ http://cdn.ac.co.uk/photos/location/$1 [NC,L,P]

RewriteCond %{HTTP_HOST} ^chi\.co\.uk$ [NC]
RewriteRule ^media/photos/(.+?\.(?:jpg|png))$ http://cdn.ac.co.uk/photos/location/$1 [NC,L,P]

RewriteCond %{HTTP_HOST} ^media\.cc\.com$ [NC]
RewriteRule ^files/assets/photos/(.+?\.(?:jpg|png))$ http://cdn.ac.co.uk/photos/location/$1 [NC,L,P]