How to avoid infinite recursion with mod_fastcgi and PHP-FPM without modifying .htaccess

Do you know this error?

the Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary

It’s really frustrating since the usual fix is to add a RewriteCond rule in the .htaccess1. However, it's not feasible when you're a hosting platform.

My goal was to add this rule without asking the user to do it himself. The solution was to add a RewriteRule server-wide and force inheritance which is not set by default.

<VirtualHost ...>
   # Your settings
   <Directory /your/directory>
      # Your settings
      <IfModule mod_rewrite.c>
         RewriteEngine on
         RewriteOptions InheritBefore
         RewriteBase /
         RewriteCond %{REQUEST_URI} ^/your-cgi-bin-fcgi-path(.*)
         RewriteRule . - [L]

This rule will simply do nothing and block all other rules (with option [L]) for this path.

With RewriteOptions Inherit, Apache will inherit server-wide rules and execute them only after the rules of .htaccess. With InheritBefore it executes them before.

Note: InheritBefore is available in Apache since 2.3.10