2014-02-23 98 views
2

我知道以前也有類似的問題,但我發現的解決方案似乎都沒有解決。我是mod_rewrite的專家,如果我錯過了一些明顯的東西,我很抱歉。正確地將一個子域名重定向到子目錄

我試圖無形地將子域重定向到子目錄中的index.php文件;此文件將子域的值作爲查詢字符串的一部分,該字段工作正常。

我遇到的問題是,現在一切該子目錄被重定向到index.php文件,我不希望發生的事情。

這是我迄今:

RewriteEngine On 
RewriteBase/

# User dashboards 
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC] 
RewriteRule ^.*$ app/index.php?user=%1 [L,NC,QSA] 

我正在尋找的是一個情況http://subdomain.example.com/會導致/app/index.php?user=subdomain,但http://subdomain.example.com/assets/stylesheet.css會去/app/assets/stylesheet.css

在此先感謝!

回答

0

如果我深知你的榜樣,你可以這樣做:

  1. 重定向的example.com www.example.com,以避免「空」子域

  2. 內部重寫每根子域(除了WWW),以/app/index.php?user=subdomain

  3. 內部改寫其他的東西與 「應用」 前綴


將由該代碼

RewriteEngine on 

# redirects example.com to www.example.com to avoid having "empty" subdomain 
RewriteCond %{HTTP_HOST} ^example.com$ 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 

# internally rewrites every root subdomains (except www) to /app/index.php?user=subdomain 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteCond %{HTTP_HOST} ^([^.]+)\. [NC] 
RewriteRule ^/?$ /app/index.php?user=%1 [L,NC,QSA] 

# internally rewrites other things with "app" prefix 
RewriteCond %{THE_REQUEST} !app/ 
RewriteRule ^/?(.+)$ /app/$1 [L,NC,QSA] 

編輯表示:當你問下面的評論,在這裏是如何也管理www子域

RewriteEngine on 

# redirects example.com to www.example.com to avoid having "empty" subdomain 
RewriteCond %{HTTP_HOST} ^example.com$ 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 

# internally redirects www subdomain root to /site/index.php 
RewriteCond %{HTTP_HOST} ^www\. [NC] 
RewriteRule ^/?$ /site/index.php [L] 

# internally rewrites every other root subdomains to /app/index.php?user=subdomain 
RewriteCond %{HTTP_HOST} ^([^.]+)\. [NC] 
RewriteRule ^/?$ /app/index.php?user=%1 [L,NC,QSA] 

# internally rewrites other things with "app" prefix 
RewriteCond %{THE_REQUEST} !app/ 
RewriteRule ^/?(.+)$ /app/$1 [L,NC,QSA] 
+0

這解決了我的問題! 是否可以指定www。子域名到不同的目錄呢? – querkmachine

+0

是的。 www子域的規則是什麼? –

+0

它可以(類似地不可見)重定向到'/ site'之類的東西嗎? – querkmachine

0

讓我們添加第二個規則,以資產重定向到應用程序/資產:

RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC] 
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|gif)$ [NC] 
RewriteRule ^.*$ app/index.php?user=%1 [L,QSA] 
RewriteRule ^assets/(.*)$ app/assets/$1 [L,QSA] 

,或者直接從應用程序加載所有的CSS/JS /圖片:

RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC] 
RewriteRule ^.*\.(css|js|png|jpg|gif)$ app/$0 [NC, QSA] 
RewriteRule ^.*$ app/index.php?user=%1 [L,QSA] 

編輯:對不起,我的天堂之前沒有測試過,所以有工作示例:

RewriteRule ^assets/(.*)$ app/assets/$1 [L,QSA] 
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC] 
RewriteRule !^app/assets/ app/index.php?user=%1 [L,QSA] 
+0

對不起,第一個例子似乎不已經改變了什麼,第二個是給我一個500內部服務器錯誤消息。 – querkmachine

+0

對不起,我添加了新的可行示例 –

相關問題