2012-07-27 31 views
0

因此,我使用的託管提供商不支持虛擬主機或任何類似的東西,所以我試圖將我的主域重寫爲子目錄。因此,例如我目前有:如何使用.htaccess重寫除子域以外的子目錄的所有流量?

example.com改寫爲example.com/sites/example.com/

這在.htaccess

Options +FollowSymLinks 
RewriteEngine On 
RewriteBase/

RewriteCond %{REQUEST_URI} !^/sites/example.com/.*$ 
RewriteRule ^(.*)$ /sites/example.com/$1 

之所以使用下面的代碼工作正常這是在同一臺服務器上有其他域,我不希望被主域訪問並保持我的服務器組織。

因此,我還將example2.com作爲指向/sites/example2.com的附加域,並且工作正常。

但是,我也有指向不同子目錄的子域,例如指向/ projects /的projects.example.com,但這些子域只是被重寫到/sites/example.com/目錄。

我的問題是我如何(也可能)解決這個問題?提前致謝!

回答

0

如果我真的明白,你應該添加主機條件,如果用戶在主域 - 他將被重定向。如果你使用 這不是優雅決定增加正則表達式域,它只會工作的二級域名,:檢查這,我已經修改了代碼,一點點:

Options +FollowSymLinks 
RewriteEngine On 
RewriteBase/
#Here we're capturing host name, if it has only one point - this is not subdomain. 
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+\.[a-z]{2,5})$ 
RewriteCond %{REQUEST_URI} !^/sites/%1/ 
#Redirecting to the current host name in folder sites 
RewriteRule ^(.*)$ /sites/%1/$1 [L] 

編輯像.co.uk這樣的域名 - 你必須像RewriteCond %{HTTP_HOST} ^somesite.co.uk$這樣的規則手動添加它們。