how to rewrite or redirect a subdomain?

Steve Chu stvchu at gmail.com
Wed Aug 8 19:25:36 MSD 2007


That's really awesome, Thanks.

We have thousands of xxx subdomains, here's our configure:

server {
        server_name  _  *.yyy.example.com;

        if ($http_host ~ "^(.+?)\.yyy\.example\.com") {
            set  $host  $1;
        }

        rewrite  ^   http://yyy.example.com/$host;
}

The "^(.+?)\." calls a dead loop, because it meet xxx.yyy.example.com,
and redirect xxx.yyy.example.com to yyy.example.com/xxx, then redirect
yyy.example.com/xxx to
yyy.example.com/yyy, then to yyy.example.com/yyy... it seems a mistake.

Does server_name  "*.yyy.example.com" match "yyy.example.com"?

On 8/8/07, Igor Sysoev <is at rambler-co.ru> wrote:
> On Wed, Aug 08, 2007 at 10:16:44PM +0800, Steve Chu wrote:
>
> > I digged the nginx rewrite module, found that it is hard to
> > rewrite/redirect a subdomain, here's my situation:
> > I wanna rewrite/redirect "http://xxx.yyy.example.com" to
> > "http://yyy.example.com/xxx".
> > I found there's a Variables $host, but how to split it to get "xxx",
> > then use rewrite module?
> > any idea?
>
>      server {
>          server_name  yyy.example.com;
>          ...
>      }
>
>      server {
>          server_name  xxx.yyy.example.com  *.www.example.com;
>
>          if ($http_host ~ "^(.+?)\.") {
>              set  $host  $1;
>          }
>
>          rewrite  ^   http://yyy.example.com/$host;
>      }
>
>
> --
> Igor Sysoev
> http://sysoev.ru/en/
>
>


-- 
Steve Chu
http://stvchu.org





More information about the nginx mailing list