title: Nginx config: route all .php files via index except if it contains a path to the file manager
body: <p>I'm working on the new project, which will have many redirects from the old website uris. Many of the old uris contain the <code>.php</code> extension in the <code>uri</code> and Nginx tries to load them as a file rather than fall back to our redirect controller method.</p>
  
  <p>To make it a bit more tricky, we are using wysiwyg editor with the file manager, which, when called - uses <code>.php</code> extension so this one needs to be excluded.</p>
  
  <p>Effectively I would like it to work the way so that when I call for instance old <code>uri</code> such as <code>/old-page/path/file.php</code> it will route it via <code>index.php</code> but when I call <code>/vendor/ckfinder/plugins/filemanager/dialog.php?...</code> it will load the actual file.</p>
  
  <p>I've seen this post, which doesn't really resolve what I'm after, but I think it's a good starting point <a href="https://stackoverflow.com/questions/21911297/how-to-remove-both-php-and-html-extensions-from-url-using-nginx">How to remove both .php and .html extensions from url using NGINX?</a></p>
  
  <p>My existing setup is</p>
  
  <pre><code>location / {
      try_files $uri $uri/ /index.php?$query_string;
  }
  
  location ~ \.php$ {
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
      fastcgi_index index.php;
      include fastcgi_params;
  }
  </code></pre>
  
  <p>Any help would be much appreciated.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I tried @Richard Smith suggestion, which after reading the docs I think should work, but unfortunately, for whatever reason - it doesn't - here's what I tried:</p>
  
  <pre><code>location / {
      try_files $uri $uri/ /index.php?$query_string;
  }
  
  location ~ \.php$ {
      try_files $uri /index.php?$query_string;
      fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
      include fastcgi_params;
  }
  </code></pre>
  
  <p>So <code>try_files</code> checks if <code>$uri</code> exists - and if it doesn't it falls back to <code>/index.php?$query_string;</code>, which should point the request to <code>index.php</code>. Any idea what I might be missing here?</p>