Using .htaccess to access environment variables in PHP

Amit Merchant · November 3, 2020 ·

If you’re working with the latest frameworks, it’s not very difficult to set up environment variables for your app.

For instance, Laravel uses the .env file to give you the ability to set the environment variables for your application.

So if you set below in the .env file…


…you can access it in the Laravel application like so.

'production' => env('APP_ENVIRONMENT', false),

But what if you want to set environment variables for the projects that use plain PHP? Read on.

Using .htaccess in Apache webserver

To define environment variables in the PHP app running on the Apache webserver, you can use the .htaccess file for this purpose. For this, all you need to enable the mod_env module in your server. You can enable using the following command.

$ sudo a2enmod env

Once done, you’ll then need to restart the server.

After this, if you want to set an environment variable called APP_ENVIRONMENT, you can add the following lines into your .htaccess file of your project like so.

<IfModule mod_env.c>
    SetEnv APP_ENVIRONMENT production

You can define as many environment variables as you may like in the same block.

To access this environment variable in your PHP application, you can use $_SERVER global array like so.


Hi there! I'm Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I'd highly appreciate that. Cheers!