2012-03-16 133 views
2

嘗試使用重寫規則解決此問題,例如,該子規則將子域分配給同名的根目錄。重寫鏈接規則,衝突的文件和文件夾

ddd.example.com會鏈接到「/ _projects/ddd」目錄,工作正常,我沒有問題,問題是我在根目錄「/」中的任何文件或目錄都可以從子域ddd.example.com訪問。

下面是一個例子的目錄結構

example.com = 「/」

  • 「的index.php」

ddd.example.com = 「/ _projects/ddd」

  • 沒有文件

所以,如果比如我訪問ddd.example.com/index.php,這將解決使用文件位於example.com/index.php這是在下面找到一個目錄。

這裏是的.htaccess

# Skip rewrite if subdomain is www 
RewriteCond %{HTTP_HOST} !^www\. [NC] 

# Extract (required) subdomain to %1 
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com(:80)?$ 

# Redirect to domain if requested URL does not resolve to existing subdirectory path 
RewriteCond %{DOCUMENT_ROOT}/_projects/%1 !-d 
RewriteRule (.*) http://example.com/ [NC,R=301] 

# Skip rewrite if subdomain is www 
RewriteCond %{HTTP_HOST} !^www\. [NC] 

# Extract (required) subdomain to %1 
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com(:80)?$ 

# Skip rewrite if requested URL does not resolve to existing subdirectory path or file 
RewriteCond %{DOCUMENT_ROOT}/_projects/%1/$1 -f [OR] 
RewriteCond %{DOCUMENT_ROOT}/_projects/%1/$1 -d 
RewriteRule (.*) /_projects/%1/$1 [NC,L] 

回答

0

解決了這個問題,似乎有是被打破了重寫一個循環的問題。

##### Subdomain to subfolder 
# Fix missing trailing slashes. 
#RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC] 
#RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC] 
#RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d 
#RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L] 

# Rewrite sub domains. 
RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC] 
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC] 
RewriteRule ^(.*)$ /projects/%2/$1 [QSA,L] 
0

如果什麼重寫規則你RewriteConds失敗?然後,URL通過並且不被重寫。所以它訪問文檔根目錄。我將爲每個受支持的子域創建單獨的VirtualHost條目。 (有多少人?)

假設客戶要求http://sub.example.com/index.php

假設存在/_projects/sub/index.php

您的RewriteCond -s會看到/_projects/sub/index.php作爲文件存在,然後跳過重寫。但是,如果重寫被跳過,那麼沒有重定向到/_projects/sub/。那麼在這種情況下提取的文件是什麼?你猜對了,/index.php

您應該無條件地將這些子域名重定向到適當的位置(僅針對循環檢查)。

爲什麼你將重寫分成兩部分,一部分做內部重定向?內部重定向不會將整個URL重寫爲example.com,因此它保留在子域中。看起來你可以在那裏進入一個循環。

+0

回覆是位於下方。 – Upperfoot 2012-03-17 14:04:34

0

我重寫的嘗試基本上是這樣做的。

僞代碼:

if (subdomain-directory != exists) 
    redirect them to the home page 
else 
    rewrite the request for the subdomain 

我只能做到使用兩個規則,我還沒有發現任何其他的方式來做到這一點,所以這是我的嘗試。

有問題的條件實際上正常工作,如果我在/ _projects/sub目錄中有一個index.php,那麼它將使用該文件,並將其放入其中的任何其他文件相同。

我完全不知道如何用mod_rewrite來完成這個任務,我已經在幾個星期的最佳時間內對它進行了無用處理,無盡地尋找可能的解決方案並且沒有取得任何進展。