title: Nginx redirect with/without php extension
body: <p>I am building an NGINX virtual host that should:</p>
  
  <ul>
  <li>redirect pages ending with .php to the version without extension (301 redirect)</li>
  <li>properly handle URLs not ending with .php by passing them to FASTCGI for php execution</li>
  </ul>
  
  <p>My issue is that this will in effect create an infinite redirect loop. What am I missing here?</p>
  
  <pre><code>server {
    server_name ~^my\.domain\.com$;
  
    listen 80;
    root /path/to/root;
  
    error_page 404 /404.php;
  
    location ~ \.php$ {
      rewrite ^(.*)\.php$ $1 permanent;
      break;
    }
  
    location / {
      index index.php;
      try_files $uri $uri/ @extensionless-php;
    }
  
    location @extensionless-php {
        rewrite ^(.*)$ $1.php last;
    }
  
    location ~ \.php$ {
          try_files $uri =404;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  
          # rest of fastcgi configuration...
    }
  }
  </code></pre>