title: Nginx location directive to transform locale in the url into a query string
body: <p>I would like to rewrite all urls like:</p>
  
  <pre><code>http://example.com/en/foo
  http://example.com/en
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>http://example.com/foo?locale=en
  http://example.com?locale=en
  </code></pre>
  
  <p>I've tried things like this (without success):</p>
  
  <pre><code>location ~ ^/([a-z]2)?/(.*)$ {
      rewrite ^([a-z]2)/(.*) /$2?locale=$1;
  } 
  </code></pre>
  
  <p>Can anyone point me to the right direction?</p>