.htaccess Set PHP value based on hostname

for our project, we need to set several PHP values ​​depending on the environment (development / production), first of all, the session saving path and some tracing and profiling parameters.

We don’t want to install them in a PHP script, because of some terrible outdated code that will require a lot of changes, and we don’t want to change .htaccess every time before passing it to git (but we require that .htaccess was in source control).

Is there a way to do something like this in .htaccess:

if (hostname == "dev.example.com") {
  php_value session.save_path /tmp
  [...]
}
+5
source share
2 answers

Apache ( .htaccess, ):

<VirtualHost *:80>                                                              
    ServerName piskvor.example.com
    ServerAlias *.host2.example.com
    php_value session.save_path /home/piskvor/tmp
</VirtualHost>
<VirtualHost *:80>                                                              
    ServerName someotherhost.example.org
    php_value session.save_path /tmp
</VirtualHost>

- ( ). Apache .

+6

:

<IfDefine SERVER1>
  php_value session.save_path /tmp1
</IfDefine>

<IfDefine SERVER2>
  php_value session.save_path /tmp2
</IfDefine>

apache :

-D SERVER1 SERVER2

EDIT:

, - ?

if (!file_exists('.htaccess')) {

  copy('.htaccess.default', '.htaccess');

  $handle = fopen('.htaccess', 'a');

  switch ($_SERVER['HTTP_HOST']) {

    case 'dev.site':
        fwrite($handle, 'php_value .....' . "\n");
        fwrite($handle, 'php_value .....' . "\n");
    break;

    case 'live.site':
        fwrite($handle, 'php_value .....' . "\n");
        fwrite($handle, 'php_value .....' . "\n");
    break;  

  }

  fclose($handle);

}
0

All Articles