Apache 2.4, mod_proxy_fcgi not honouring .htaccess, work around needed

.htaccessapache-2.4mod-proxy-fcgiphp-fpm

I am using apache 2.4.7 with mod_proxy_fcgi for purpose of passing
through php to php-fpm (this will be used for shared hosting
environment).
The htaccess works fine for non php files, but once it hit rewrite
rule that proxies through the php requests, the htaccess is ignored.

I know why it is happening. The question is: how do I work around it?

The question how do I force apache to treat the request to php file as
a request to local file, and then proxy it through?

I have spent substantial time in researching on this problem, and
following "answers" were given as solution:

1) "use apache configuration instead of .htaccess" it is valid
solution, but not for shared hosting environment (I am not going to
give access to apache configuration to shared hosting customers ;)).

2) "don't use .htaccess, as it has performance/security/other issues",
well how else would shared hosting customers control access/url
rewriting on their site? Besides if the .htaccess was not a
requirement I would simply use nginx.

3) "put rewrite rule for proxy inside of " – this is
incorrect, and it does not work.

This behaviour appears to be not a bug but a "feature" as per
https://issues.apache.org/bugzilla/show_bug.cgi?id=54887

Best Answer

Meanwhile, another option is available since Apache 2.4.10: Proxy via Handler. See the example in the Apache documentation: mod_proxy_fcgi examples

You can also force a request to be handled as a reverse-proxy request, by creating a suitable Handler pass-through. The example configuration below will pass all requests for PHP scripts to the specified FastCGI server using reverse proxy. This feature is available in Apache HTTP Server 2.4.10 and later. For performance reasons, you will want to define a worker representing the same fcgi:// backend. The benefit of this form is that it allows the normal mapping of URI to filename to occur in the server, and the local filesystem result is passed to the backend. When FastCGI is configured this way, the server can calculate the most accurate PATH_INFO.

<Proxy "fcgi://localhost/">
    ProxySet enablereuse=On
</Proxy>
<FilesMatch "\.php$">
    SetHandler "proxy:fcgi://localhost:9000"
</FilesMatch>

This allows RewriteRules and authentication in .htaccess files. After all the rewriting is done, the request is passed to php-fpm.