title: Nginx, rewrite multiple subfolders
body: <p>I try to move my server from Apache towards a Nginx based setting. However, I get into problems getting a part of my htaccess Apache magic to work in Nginx. </p>
  
  <p>The .htaccess code I would like to transfer is:</p>
  
  <pre><code>RewriteRule ^([a-zA-Z0-9_]+)/$                  /index.php?page=$1 [L,QSA] 
  RewriteRule ^([a-zA-Z0-9_]+).html$              /index.php?page=$1 [L,QSA] 
  RewriteRule ^([a-zA-Z0-9_]+)/([0-9]+)/.*/([a-zA-Z0-9_]+)$       /index.php?page=$1&amp;id=$2&amp;sub=$3 [L,QSA]
  RewriteRule ^([a-zA-Z0-9_]+)/([0-9]+)/.*$       /index.php?page=$1&amp;id=$2 [L,QSA] 
  </code></pre>
  
  <p>I used an <a href="http://winginx.com/en/htaccess" rel="nofollow">online converter</a> that gave me the following location block:</p>
  
  <pre><code>location / {
  rewrite ^/([a-zA-Z0-9_]+)/$ /index.php?page=$1 break;
  rewrite ^/([a-zA-Z0-9_]+).html$ /index.php?page=$1 break;
  rewrite ^/([a-zA-Z0-9_]+)/([0-9]+)/.*/([a-zA-Z0-9_]+)$ /index.php?page=$1&amp;id=$2&amp;sub=$3 break;
  rewrite ^/([a-zA-Z0-9_]+)/([0-9]+)/.*$ /index.php?page=$1&amp;id=$2 break;
  }
  </code></pre>
  
  <p>Sadly, I'm only able to get a single location rewrite to be working at a time (the one I put "last;" behind). I don't seem to be able to do this dynamic rewriting of a multiple subfolder URL into arguments for a single PHP script. </p>
  
  <p>Any help on this? What is the Nginx way of doing such a rewrite? I tried using several location's for different subfolders but I would rather have a generic solution that work no-matter what url's are thrown at it. </p>