2011-01-28 60 views
1

我需要一个未知的子域转换为一个变量,因此以下将适用:现代重写 - 高级问题

  • http://test.example.com/http://www.example.com/?domain=test

  • http://xyz.example.com/http://www.example.com/?domain=xyz

  • http://www.example.com/http://www.example.com/

  • http://www.example.com/pageA/http://www.example.com/pageA/

  • http://fish.example.com/pageB?somevar=somethinghttp://www.example.com/pageB?somevar=something&domain=fish

  • http://www.example.com/pageB?somevar=somethinghttp://www.example.com/pageB?somevar=something

  • http://fish.example.com/pageBhttp://www.example.com/pageB?domain=fish

正如你可以看到所有我需要做的是更换任何子站点www并将子域名添加为名为的域名为的获取变量。

我真的失去了这个。

编辑:哦我还希望用户仍然可以看到url中的子域,而不是重定向。

回答

1

试试这个:

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^([a-z]+)\.example\.com$ 
RewriteCond %1 !=www 
RewriteRule^http://www.example.com%{REQUEST_URI}?domain=%1 [QSA] 
+0

这会保留其他的变量吗?例如?var = test&domain = xx – DwayneBull 2011-01-28 14:32:28

0

我会用:

RewriteEngine on 
RewriteCond %{HTTP_HOST} !=www.example.com 
RewriteCond %{HTTP_HOST} ^(www\.|)(.+)\.example\.com$ 
RewriteRule ^.*$ http://www.example.com/$1?domain=%2 [QSA] 

这也将改写www.fish.example.com...?domain=fish