情景如下:
http://sh.test.local 指向 test_sh 目錄
http://gz.test.local 指向 test_gz 目錄
使用域名 http://img.test.local專門用來放圖片,以便瀏覽器加載的,
但事實上是, http://img.test.local 是要根據不同主訪問來指定不同的圖片目錄的,比如說,
通過 http://sh.test.local 的時候,訪問的是 test_sh/upload/images 里面的文件
通過 http://gz.test.local 的時候,訪問的是 test_gz/upload/images 里面的文件
這時候,就要通過迂回的方法來做了。
創建目錄 test_img,apache創建virtualhost,指到 test_img,如
<VirtualHost *:80>
<Directory " /path/to/webroot/test_img ">
</Directory>
DocumentRoot " /path/to/webroot/test_img "
ServerName img.test.local
ServerAlias img.test.local
</VirtualHost>
<Directory " /path/to/webroot/test_img ">
</Directory>
DocumentRoot " /path/to/webroot/test_img "
ServerName img.test.local
ServerAlias img.test.local
</VirtualHost>
在 test_img 下面建立文件 .htaccess,內容如下:
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://gz.test.local/.*$ [NC]
RewriteRule ^(.*)$ http://gz.test.local/ $1 [R=301,L]
RewriteCond %{HTTP_REFERER} ^http://sh.test.local/.*$ [NC]
RewriteRule ^(.*)$ http://sh.test.local/ $1 [R=301,L]
RewriteCond %{HTTP_REFERER} ^http://gz.test.local/.*$ [NC]
RewriteRule ^(.*)$ http://gz.test.local/ $1 [R=301,L]
RewriteCond %{HTTP_REFERER} ^http://sh.test.local/.*$ [NC]
RewriteRule ^(.*)$ http://sh.test.local/ $1 [R=301,L]
記得確保你的 apache 支持 rewrite,同時還要確定在 apache的配置文件里面是:
AllowOverride all
重啟 apache
sudo /etc/init.d/apache2 restart
搞掂!