[Ubuntu] apache .htaccess根據訪問的域名指向不同的目錄


情景如下:

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

 

搞掂! 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM