nginx using OR set at regex for the map

Viewed 24170

I'm trying to write a map for my website and it's working great:

map $request_uri $redirect_uri {
  /en/oldname    /en/newname;
  /de/oldname    /de/newname;
  /fr/oldname    /fr/newname;
}

until I try to implement some regex, something like this:

map $request_uri $redirect_uri {
  /(?<lang>(en|de|fr))/oldname    /$lang/newname;
}

The map above is not working, and due to the lack of debugging knowledge - I'm unable to know why. Even the basic regex (without using named captures) is not working for me:

map $request_uri $redirect_uri {
  /(en|de|fr)/oldname    /en/newname;
}

nginx 1.10.3

Please help me to figure out what am I doing wrong?

3 Answers

In regards to mononoke's answer. You don't need the extra set of parentheses in the pattern matching, and that could give some unexpected results for certain pcre engines as you are technically adding another group within the lang group.

~/(?<lang>en|de|fr)/oldname    /$lang/newname;
map $http_user_agent $loggable {
    "~kube-probe"   0;
    default         1;
}
Related