您的位置:首页 > 运维架构 > Apache

[Ubuntu] apache .htaccess根据访问的域名指向不同的目录

2012-12-03 18:04 477 查看
情景如下:
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

搞掂!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: