既然你是重定向到另一個網站,我相信你需要調用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();
可以使用:'重寫規則^(媒體/.*)http://anothersite.com/$1 [R = 301,NC,L]' – anubhava
論'/媒體的請求/ image1.jpg'例如,你想服務於'http:// anotheriteite.com/media/image1.jpg'或'http:// anotheriteite.com/image1.jpg'中的內容嗎?問題中的第一句話告訴你想要第一個URL,'RewriteRule'讓你成爲第二個URL。哪一個? – axiac
@axiac後者 - 現在覆蓋着GentlemanMax的回答 – technopeasant