0

I have nginx configuration like this:

map $request_uri $target {
    /test1234 https://www.somedomain.com/new_url?source=stackoverflow$is_args$args;
}

map $request_uri $target_code {
    /test1234 301;
}
server {
    listen 80 default;

    if ($target_code = 301) {
        return 301 $target;
    }
}

For /test1234 it works but if I have /test1234?test=1 or any query string, then nginx doesn't match this redirect. It must work for any parameters.

Is any way to set up wildcard for any query string?

1 Answer 1

3

First, you are using $request_uri variable, which contains full original request URI with arguments, so your map directive will never match when request have some parameters. Use $uri variable instead.

Secondly, imagine you have a request http://yourdomain.com/test1234?test=test. In $target you'll get https://www.somedomain.com/new_url?source=stackoverflow?test=test. You can use additional map directive in your config to avoid that:

map $is_args $args_prefix {
    ? &;
}

map $uri $target {
    /test1234 https://www.somedomain.com/new_url?source=stackoverflow$args_prefix$args;
}

And at last, I recommend use of location directive inside your server configuration block instead of if, for example:

location = /test1234 {
    return 301 $target;
}

If is evil!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is what I needed. But I have to using if because I have different redirect types: 301 302 303 and 307

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.