情景如下:

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> 

在 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]

 

记得确保你的 apache 支持 rewrite,同时还要确定在 apache的配置文件里面是: 

AllowOverride all 

 

重启 apache

sudo /etc/init.d/apache2 restart

 

搞掂! 

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2021-06-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-08
  • 2022-12-23
  • 2021-11-09
  • 2022-12-23
相关资源
相似解决方案