title: nginx redirect multiple URLs with map
body: <p>I am trying to redirect a bunch of old URLs into new URls with the map directive. I am able to do the simple ones but kinda stuck on the ones with query parameters. </p>
  
  <p>I need to do this</p>
  
  <pre><code>/people.php?personid=20 -&gt; /people/20
  /events.php/eventid=20 -&gt; /event/20
  /info.php?name=john&amp;age=20 -&gt; /person/john/20
  </code></pre>
  
  <p>I've got a map directive</p>
  
  <pre><code>map $request_uri $redirected_uri {
  ~^people.php\?personid=(.*)^ /people/$1?;
  }
  </code></pre>
  
  <p>But doesn't seem to work. Any help would be appreciated. Thanks in advance.</p>
  
  <p>My server block has this code</p>
  
  <pre><code>if ($redirected_uri) {
     rewrite  ^ $redirected_uri permanent;
  }
  </code></pre>
  
  <p>Also, I am trying to do the following as well
  rewrite /people/20 back to people.php?personId=20
  So, for that, I have this </p>
  
  <pre><code>map $request_uri $new_uri {
  default 0
  ~^/people/(.*) /people.php?personId=$1;
  }
  </code></pre>
  
  <p>and then in server block</p>
  
  <pre><code>if ($new_uri) {
  rewrite ^ $new_uri last; // this doesn't work, throws 404
  }
  </code></pre>