0

I'm using nginx and its map module (http://wiki.nginx.org/HttpMapModule) to create rewrites depending on $args value. My config looks like the following:
nginx.conf

map $args $myvar {
    default 0;
    include /path/to/file.conf;
} 

file.conf has something like:

foo=1 /url1;
foo=2 /url2;
....
foo=n /urln;

afterwards I'm using $myvar for redirects like this

location = /somepage.html {
    if ($myvar !~ 0) {
        rewrite ^ $myvar? permanent;
    }
    rewrite ^ /mydefaultpage/? permanent;
} 

The question is: can I use regex inside my file.conf, which I include inside my map directive? So I could write something like

~^foo=1(&<suffix>.*)$ /url1?$suffix;

and thus preserve all other args when redirecting to /url1.

Thanks in advance for your help!

1 Answer 1

1

You can't use /url1?$suffix in the second argument. It must either be variable or static string.

Try this:

map $arg_foo $redirect_to {
    default /mydefaultpage/;
    include /path/to/file.conf;
}

location = /somepage.html {

    set $redirect_args $args;

    if ($args ~ "^(?:(.+)&)?foo=[^&]*(?:&(.+))?$") {
        set $redirect_args $1&$2;
    }

    return 301 $redirect_to?$redirect_args;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.