2012-08-06 125 views
0

我有這方面的工作:麻煩子域名重定向到一個文件夾

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk 
RewriteRule ^(.*) http://domain.co.uk/%1 [L] 

所以會重定向到test.domain.co.uk test.domain.co.uk/test

然而,我會喜歡它,以便它更改文檔根目錄而不是用戶看到重定向。我在這裏閱讀了一些文章並嘗試了一些東西。但我不知道如何調試任何錯誤,它只是不起作用。另一個,我曾嘗試:

RewriteEngine On 
RewriteBase/
RewriteCond %{REQUEST_URI} !^/- 
RewriteCond %{HTTP_HOST} ^([^\./]+) 
RewriteCond %{DOCUMENT_ROOT}/-%1 -d 
RewriteRule ^(.*)$ -%1/$1 [L] 

但這似乎並不工作。

任何幫助,將不勝感激。

伊恩

+0

'-'的用途是什麼? – 2012-08-06 16:24:05

+0

我不確定,他們在我使用的示例中。 – 2012-08-07 07:59:38

回答

1

嘗試這樣的事情在你的文檔根:

RewriteEngine On 

# check if subdomain folder has the existing file, if so, serve it 
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk 
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -f 
RewriteRule ^(.*)$ /%1/$1 [L] 

# check if subdomain folder has the existing directory WITH A TRAILING SLASH, if so serve it 
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk 
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d 
RewriteRule ^(.*?)/$ /%1/$1/ [L] 

# check if subdomain filder has the existing directory but missing trailing slash, redirect with it 
RewriteCond %{REQUEST_URI} !/$ 
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk 
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d 
RewriteRule ^(.*)$ /$1/ [R=301,L] 

該方法做兩件事情對你來說,如果它是一個404,它不會試圖在內部重寫,所以如果你去:http://sub.domain.co.uk/this/path/does/not/exist/blah,你不會得到一個404消息說:/sub/this/path/does/not/exist/blah不存在,只是/this/path/does/not/exist/blah,因爲該URI沒有被重寫,因此不公開您的基礎目錄結構。

第二件事是你可能打開了DirectorySlash。這意味着如果你訪問一個目錄,如:http://sub.domain.co.uk/this/path,缺少斜線,mod_dir會重定向並添加該斜線,但它可能會做錯,因爲URI已被重寫並將重定向到:http://sub.domain.co.uk/sub/this/path/。我們先發制人地在後面加上正確的URI來隱藏sub

+0

嘿,這似乎並沒有工作,有無論如何,我可以嘗試和調試,如果有任何錯誤顯示? 我在最後添加了一條重寫規則,它被執行,所以它實際上並不符合任何條件。 – 2012-08-07 07:56:34

+0

@IanJamieson是房東配套嗎? – 2012-08-07 16:26:36

+0

是的,它匹配的主機,我測試通過添加一個重定向到谷歌,如果它匹配的HTTP主機,它! – 2012-08-09 09:56:54

相關問題