.htaccessconditional-statementsdetectapache2.4mod-php

htaccess - detect mod_php


I'm trying to limit/eliminate duplication.

According to my (limited) understanding of some LAMP hosting providers (especially on shared servers) there are some PHP-ini directives that are ignored when set inside the PHP-script, but is accepted when set in the .htaccess; the process-flow should be quite simple:

The objective is to withhold all content unless PHP is available to handle the request according to config -- per user/bot/api/etc. ..and by this obviously not serve any PHP-code as text O_o

I have no idea why this has to be any more complicated, but I refuse to accept that I have to duplicate a (long) list of php_value directives 4 times -- just because the Apache-conf is too (expletive deleted) to handle the simplest logical conditions .. (face-desk) .. or maybe i just don't know enough about Apache-conf, please help; the code below throws a 400 error, and I can't get it to work unless I duplicate each block for every php-version -and each hosting provider's own mod_php*suffix -for PHP-version-hot-switching.

Here's what [bl/thr]ows:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    <IfModule mod_php_null.c>
        SetEnv MODPHP 1
    </IfModule>
    <IfModule mod_php5.c>
        SetEnv MODPHP 1
    </IfModule>
    <IfModule mod_php7.c>
        SetEnv MODPHP 1
    </IfModule>

    <If "%{ENV:MODPHP} =1">
        php_value expose_php          Off
        php_value short_open_tag      On
        php_value display_errors      On
        php_value max_execution_time  60
        php_value default_charset     UTF-8
        # list shortened for brevity

        RewriteCond %{DOCUMENT_ROOT}/.auto.php -f
        RewriteRule ^(.*)$ .auto.php [L]
    </If>
</IfModule>

Solution

  • The code below works, however; note that:


    <IfModule mod_rewrite.c>
        <IfModule mod_authz_core.c>
            RewriteEngine On
            RewriteBase /
    
            <IfModule mod_php_null.c>
                RewriteRule .* - [E=MODPHP:1]
            </IfModule>
            <IfModule mod_php5.c>
                RewriteRule .* - [E=MODPHP:1]
            </IfModule>
            <IfModule mod_php7.c>
                RewriteRule .* - [E=MODPHP:1]
            </IfModule>
    
            <If "%{ENV:REDIRECT_MODPHP} =1">
                php_value expose_php          Off
                php_value short_open_tag      On
                php_value display_errors      On
                php_value max_execution_time  60
                php_value default_charset     UTF-8
            </If>
    
            RewriteCond %{ENV:MODPHP} ^1$
            RewriteCond %{DOCUMENT_ROOT}/.auto.php -f
            RewriteRule ^(.*)$ .auto.php [L]
        </IfModule>
    </IfModule>