Home > Software engineering >  htaccess SetEnv directive is not working in MAMP pro 6
htaccess SetEnv directive is not working in MAMP pro 6

Time:01-16

I have a solution develop with PHP

  1. Framework Phalcon 3.4.5
  2. macOS 12.6
  3. PHP 7.3.33
  4. MAMP Pro 6

I have this .htaccess that is in my root folder:

A SetEnv directive is set like that:

<IfModule mod_env.c>
SetEnv APPLICATION_ENV=development
</IfModule>

The right env module are loaded in Apache:

  1. mod_env.so
  2. mod_setenvif.so

My host config have AllowAoverride All My AccessFileName is set to htaccess

I use this code in my index file to read the environment variable and I can't get the value.

defined('ENVIRONMENT') || define('ENVIRONMENT', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

echo ENVIRONMENT . PHP_EOL;

Considering the SetEnv in the htaccess, should get development has a value, but I keep getting production.

I did test to be sure that mod_env and mod_setenvif are loaded. There is no error in my apache log and php log.

I have no clue why. Any help will be welcome here?

Thank you!

CodePudding user response:

Environment variables set with SetEnv in an htaccess file are available in the Apache environment, so you should use apache_getenv() or the $_SERVER superglobal to access the variable rather than getenv().

You appear to be using SetEnv correctly, and the rest of your setup looks fine.

Perhaps you may be interested in a .env file instead in your root directory, and using vlucas/dotenv to load it in, and use Dotenv in your code instead.

Here's an example of using that package:

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

It will look for an .env file in __DIR__ and load them into your environment, making them available via getenv()

  • Related