2015-12-01 72 views
1

我處於用戶可以在子域中創建其博客的情況。將子域通配符重寫爲子目錄

用戶將創建的博客,並輸入他想要像說的地址,

abcd.domain.com然後我的PHP代碼創建了一個名爲abcd目錄。

查看博客用戶將鍵入在他的瀏覽器abcd.domain.com,我想一個.htaccess代碼將改寫的URL,然後打開domain.com/abcd 裏面的文件,但用戶在瀏覽器的URL應該保持abcd.domain.com

目前我想這個代碼

RewriteCond %{HTTP_HOST} ^test\.domain\.com$ 
RewriteCond %{REQUEST_URI} !^test/ 
RewriteRule ^(.*)$ /test/$1 [L,QSA] 

但是這給了我404,即使我有test文件夾中的文件test.html並試圖查看該頁面。

也在這種情況下,我將不得不手動更改.htaccess文件的URL重寫。我想知道的是,如果可以將通配符子域重定向到相應的目錄。

回答

2

您可以使用:

RewriteCond %{HTTP_HOST} ^test\.domain\.com$ [NC] 
RewriteCond %{REQUEST_URI} !^/test/ 
RewriteRule ^(.*)$ /test/$1 [L,QSA] 

REQUEST_URI領先/

憑藉外卡子域:

RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC] 
RewriteCond %{REQUEST_URI} !^/%1/ 
RewriteRule ^(.*)$ /%1/$1 [L,QSA] 
+0

雖然代碼的第一部分解決了測試文件夾的404,但通配符的第二部分不起作用。當我嘗試使用第二個.htaccess代碼重新加載與第一個代碼一起工作的相同url時,我收到錯誤消息msg –

+0

'Not Found 請求的URL /test/test/redirect:/test/test/test.html/redirect: /test/test/test.html/redirect:/test/test/test.html/redirect:/test/test/test.html/test.html/test.html/redirect:/test/test/test.html/在此服務器上找不到test.html/test.html/redirect:/test/test/test.html/。 –

+0

您只使用第二部分? (並刪除測試部分) – Croises

1

注意,它需要比重寫規則更加有通配符子域。只是fyi。

您需要爲子域創建一個wildcard DNS record,並通過在Apache配置中使用ServerAlias的來告訴apache使用任何子域請求。

然後試試你的規則,看看它是否適合你。

RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$ [NC] 
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/? 
RewriteRule ^(.*)$ /%1/$1 [L,QSA] 
+0

我已經有'wildcard DNS active' –

+0

好吧......你試過規則嗎? –

+0

試過你的代碼,給出了500錯誤。 :( –