nginx: rewrite a LOT (2000+) of urls with parameters

You cannot match the query string (anything from the ? onwards) in location and rewrite expressions, as it is not part of the normalized URI. See this document for details.

The entire URI is available in the $request_uri parameter. Using $request_uri may be problematic if the parameters are not sent in a consistent order.


To process many URIs, use a map directive, for example:

map $request_uri $redirect {
    default 0;
    /somepath/somearticle.html?p1=v1&p2=v2  /some-other-path-a;
    /somepath/somearticle.html              /some-other-path-b;
}

server {
    ...
    if ($redirect) {
        return 301 $redirect;
    }
    ...
}

You can also use regular expressions in the map, for example, if the URIs also contain optional unmatched parameters. See this document for more.

Leave a Comment