views:

686

answers:

1

Hi, what I'm looking for is the ability for nginx to detect that a url has the query string variable cid.

for example www.adomain.com/froggy?cid=12&as=false (query string can be solo or mixed with others and cid is not necessarily the first variable in the query string)

If cid is detected, it must send out a set cookie header with the variable tmp_cid giving it the value it took from the cid query string variable.

It must then also send out a 301 redirect to send the user to the same url it came in on but without the cid query string variable, so with the above example url, it would redirect to www.adomain.com/froggy?as=false

I hope this makes sense.

+1  A: 
location / {

    set $cid '';

    if ( $query_string ~ "(.*)cid\=([0-9]+)(&?)(.*)" ) {
        set $args $1$4;
        set $cid $2;
    }

    if ( $args ~ (.+)&$ ) {
        set $args $1;
    }

    if ( $cid != '' ) {
        add_header Set-Cookie tmp_cid=$cid;
        rewrite ^(.*)$ $1 permanent;
    }

}
Damian Casale