2015-07-03 89 views
1

我有多個子域名,均使用通用虛擬服務器和入口點/var/www/index.php,此文件使用$_SERVER['HTTP_HOST']來確定用於控制生成的HTML的適用「站點」。通配符子域名的單個資源

site1.example.com 
site2.example.com 
site3.example.com 
... 
siteN.example.com 

<VirtualHost *:443> 
    ServerName example.com 
    ServerAlias *.example.com 
    <Directory "/var/www"> 
    </Directory> 
</VirtualHost> 

現在,我希望每個這樣的子域名站點都有獨立的公共圖像資源可供他們使用。要做到這一點,我目前只是增加了單個目錄爲每個網站,其中提供給所有網站

/var/www/public/site1/image1.png 
/var/www/public/site1/image2.png 
/var/www/public/site1/image3.png 

/var/www/public/site2/image4.png 
/var/www/public/site2/image5.png 

然後用PHP編寫一個基於$_SERVER['HTTP_HOST']適當的路徑。

<img src="/public/site1/image1.png" alt="bla" > 

鑑於這種情況,是否有可能使用Apache(或者PHP或Linux的符號鏈接)不需要其中的子域?

<img src="/public/image1.png" alt="bla" > 

回答

1

它必須使用.htacces才能實現。您需要實現以下幾點:

if($domain is 'site1.host.com') 
{ 
    rewrite '/public/image1.png' to '/public/site1/image1.png' 
} 

這不是測試,可能需要添加其他圖片擴展:

RewriteCond %{HTTP_HOST} ^(.+)\.host\.com [NC] 
RewriteRule ^/public/(.*\.png)$ /public/$1/$2 [NC,L] 

編輯。以下似乎工作。

RewriteCond %{HTTP_HOST} ^(.+)\.host\.com [NC] 
RewriteRule ^public/([^//]+)$ public/%1/$1 [NC,L] 
+0

謝謝。可以用通配符而不是硬編碼'site1'完成嗎? – user1032531

+0

@ user1032531我不確定,1美元和2美元都可以工作。但這是我們必須採取的行動。 PLZ,嘗試更新的htaccess。 – user4035

+0

經過測試,但不起作用。日誌顯示每個目錄帶,然後應用模式,但通過,並導致缺少文件。 – user1032531