如何使用/media/将/media/中的所有文件定向到http://anothersite.com/media?我正在引导一个中转站点的图像请求到它的主站点的目录。
下列措施不起作用:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^media/(.*) http://anothersite.com/$1 [QSA,L]
</IfModule>Apache并没有太多混乱--如果这是一个蹩脚的问题,请原谅我。
发布于 2015-08-26 18:33:47
因为您要重定向到另一个站点,所以我认为您需要调用mod_proxy而不是mod_rewrite。您可以通过将[QSA, L]更改为[P]来做到这一点。所以就像这样:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^media/(.*) http://anothersite.com/media/$1 [P]
</IfModule>否则,你的重写规则看上去不错。
一个可能更好的替代方案是一个本地PHP脚本,它加载并返回重定向到的映像。
在这种情况下,重写规则如下所示:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^media/(.*) /imageFetcher.php?img=$1 [QSA, L]
</IfModule>然后需要创建一个文件imageFetcher.php。使用以下内容
<?php
//Do some checks to make sure this request came from your site, you won't want external users accessing this script
$img_file = $_GET['img'];
$img_data = file_get_contents("http://anothersite.com/$img_file");
//Possibly verify that $img_data is a valid image file using imgjpeg(), imgpng(), etc
header('Content-Type: image/jpeg'); //This assumes your image is a jpeg. If the image could be a png/gif/etc you'll need to do some logic to set the proper header.
echo $img_data;
exit();https://stackoverflow.com/questions/32234054
复制相似问题