title: Nginx rewrite "directory" language to query string
body: <p>I'm trying to get Nginx to rewrite a fake directory to a query string to be used as the language.  For example:</p>
  
  <pre><code>/fr/example.php
  </code></pre>
  
  <p>should look like that in the URL, but should rewrite to</p>
  
  <pre><code>/example.php?language=fr
  </code></pre>
  
  <p>Here is the code I have tried in my Nginx config:</p>
  
  <pre><code>rewrite "^(/(fr|en))?/(.*).php$" /$3.php?language=$2 last;
  </code></pre>
  
  <p>When I visit <code>example.php</code>, I get the correct page, but when I visit <code>/fr/example.php</code>, I get a 404 error.</p>
  
  <p><strong>Edit:</strong> This seems to be somehow related to the fact that it is a <code>.php</code> extension.  If I try with .html instead, it works.  But unfortunately I need it to be php.  Here is a little more of my <code>nginx.conf</code> that might be helpful, found just a little later than the code I posted above:</p>
  
  <pre><code>location / {
      root   /var/www/example.com;
      index  index.php index.html index.htm;
  
      rewrite "^(/(fr|en))?/(.*).php$" /$3.php?language=$2 last;
  }
  
  error_page 404 /404.php;
  location /404.php {
      root /var/www/example.com;
  }
  
  location ~ \.php$ {
      include snippets/fastcgi-php.conf;
  
      # CUSTOM
      fastcgi_connect_timeout 60;
      fastcgi_send_timeout 180;
      fastcgi_read_timeout 180;
      fastcgi_buffer_size 128k;
      fastcgi_buffers 256 16k;
      fastcgi_busy_buffers_size 256k;
      fastcgi_temp_file_write_size 256k;
      fastcgi_intercept_errors on;
      # END CUSTOM
  
      # With php7.0-fpm:
      fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  }
  </code></pre>