Regex for rewrite of subdomains

Igor Sysoev igor at sysoev.ru
Fri Feb 15 06:21:48 UTC 2013


On Feb 15, 2013, at 9:48 , Darren Pilgrim wrote:

> On 2013-02-14 14:05, etrader wrote:
>> I want to rewrite subdomains by adding a separate server for subdomains as
> [...]
> > keyword.domain.com/query=some to
> > domain.com/script.php?query=some&sub=keyword
> 
> server {
>  server_name *.domain.com;
> 
>  if ($http_host ~* (.+)\.domain.com$) {
>    set $keyword $1;
>  }
> 
>  if ($request_uri ~* ^/query=(.+)) {
>    return 301 http://domain.com/script.php?query=$1&sub=$keyword;
>  }
> }

Oh, NO!

server {
    server_name  ~^(?<KEYWORD>.+)\.domain\.com$;
    return 301  http://domain.com/script.php?query=$arg_query&sub=$keyword;
}

Or if the server may process something expect "query":

server {
    server_name  ~^(?<KEYWORD>.+)\.domain\.com$;

    if ($arg_query) {
        return 301  http://domain.com/script.php?query=$arg_query&sub=$keyword;
    }
    ...
}


--
Igor Sysoev
http://nginx.com/support.html



More information about the nginx mailing list