Forum: NGINX Regex for rewrite of subdomains

Posted by etrader (Guest)
on 2013-02-14 23:05
(Received via mailing list)
I want to rewrite subdomains by adding a separate server for subdomains 
as


server {
server_name domain.com
...
}
server {
server_name *.domain.com
}

but how I can I ass a rewrite rule int the second server to process
subdomain requests as

keyword.domain.com/query=some to
domain.com/script.php?query=some&sub=keyword

Posted at Nginx Forum: 
http://forum.nginx.org/read.php?2,236231,236231#msg-236231
Posted by Steve Holdoway (Guest)
on 2013-02-14 23:51
Attachment: smime.p7s (6,04 KB)
(Received via mailing list)
I'd take a look at using pattern matching in a map
( http://wiki.nginx.org/HttpMapModule ) and redirecting if the default
value isn't found? Maybe not the most effective, but simpler to
maintain...

Steve
Posted by Darren Pilgrim (Guest)
on 2013-02-15 06:48
(Received via mailing list)
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;
   }
}

I'm not sure a map would be faster in this case because you don't need a
multitude of patterns.
Posted by Igor Sysoev (Guest)
on 2013-02-15 07:22
(Received via mailing list)
On Feb 15, 2013, at 9:48 , Darren Pilgrim wrote:

>    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=...
}

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=...
    }
    ...
}


--
Igor Sysoev
http://nginx.com/support.html
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.