2011-04-14 33 views
2

我一直在尋找互聯網,試圖找到這個問題的解決方案,但找不到明確的解決方案。使用htaccess將所有虛擬子域重寫爲相同的文件夾

我想 重定向 改寫

anything.domain.com/anypage.php?sub=anything &包括= get_parameters

域。 com/users/anypage.php?sub = anything & includes = get_parameters


我已經發現了幾頁有可能的解決方案,但他們都略有不同,以我的需求,並進行編輯以失敗結束持續。

任何幫助將不勝感激。謝謝。

P.S通配符DNS已啓用,並且所有內容都在apache中正確設置。

編輯:

我結束了使用如下因素,似乎工作得很好。謝謝。

RewriteCond %{HTTP_HOST} 
^(.*).domain.com RewriteCond 
%{HTTP_HOST} !^www.domain.com [NC] 
RewriteRule ^(.*)$ 
http://domain.com/users/$1?sub=%1 [P] 

回答

4

嘗試在你的.htaccess文件中這樣的規則:

Options +FollowSymLinks 
RewriteEngine on 

# redirect for http 
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC] 
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC] 
RewriteCond %{SERVER_PORT} =80 
RewriteRule ^(.*)$ http://domain.com/users/$1?sub=%1 [R=301,QSA,L,NE] 

# redirect for https 
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC] 
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC] 
RewriteCond %{SERVER_PORT} =443 
RewriteRule ^(.*)$ https://domain.com/users/$1?sub=%1 [R=301,QSA,L,NE] 

R=301 will redirect with https status 301 
L will make last rule 
NE is for no escaping query string 
QSA will append your existing query parameters 

$1 is your REQUEST_URI 
+0

我應該在這個問題已指定由「什麼」,我的意思是,這個值是一個變量(使用者名稱是精確)。 – DaveE 2011-04-14 02:14:08

+0

@dave e:我按照你的評論編輯了我的答案。 – anubhava 2011-04-14 02:17:02

+0

你不必逃避。在方括號內使用文字值。但這應該工作。並且在'$ 1'前添加'users /'以匹配示例。尼斯點添加HTTPS。 – 2011-04-14 09:29:46

相關問題