如何将子域redirect到以子域为参数的域

我想redirect我的通配符子域到我的主域使用.htaccess的URL中的子域作为GET参数。

我想要发生的事情的例子:

 billy.example.com 

redirect到

 www.example.com/profile?user_name=billy 

以下是我基于本网站上的其他答案放在一起的现行规则:

 RewriteCond %{HTTP_HOST} ^(.*).example.com RewriteRule ^(.*)$ http://www.example.com/shop?user_name=%1 [R,L] 

但是,当redirect发生时,我得到以下URL“redirect循环”错误:

 www.example.com/profile?user_name=www 

我已经看到这个网站回答了几个不同的方式,但不幸的是他们都没有为我工作。

(我正在使用PHP,并且我在主机面板中设置了*作为子域。)

你的redirect循环正在发生,因为你redirect了所有的东西(甚至www.example.com)。 也许尝试像这样:

 RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC] RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC] RewriteRule ^/(.*)$ http://www.example.com/shop?user=%1 [L,R=301] 

只有当您访问http://www.example.com以外的网站时,才会触发此操作。

在尝试使用.htaccess和失败解决这个问题后,我最终使用PHP为我做了URLparsing。 对于那些希望看到我做了什么的人:

 $ host = $ _SERVER ['HTTP_HOST'];
 $ subdomain = str_replace('。example.com','',$ host);
如果($ subdomain!='www')
    标题(“位置:http://www.example.com/shop?user_name=".$subdomain);