# Environment detection with .htaccess

Source: https://docs.fortrabbit.com/dev/htaccess/environment-detection
Reviewed: 2026-07-07

> Apply .htaccess rules per environment: set an environment variable in the dashboard, then branch on it to force HTTPS only in production.


## 1. Make sure environment variables are set

To enable environment-specific `.htaccess` rules, you first need to set an environment variable in the fortrabbit dashboard. In the dashboard with your fortrabbit environment, make sure an ENV var key-value pair like this is present. See [the .htaccess intro](/dev/htaccess/intro) for context on how `.htaccess` rules work:



```shell
ENVIRONMENT=production
```

In some cases we have pre-populated that for you already. See [environment variables topic](/dev/env-vars/intro) for more details on how environment variables are handled here.

## 2. Setup .htaccess files

Now in the local code base of your project, setup a custom `.htaccess` file for each environment in the web root of your project:

- `.htaccess` in general and for local development
- `.htaccess_production` for your fortrabbit App

This can also be extended to more environments. All `.htaccess` files should be under version control with Git.

## 3. Configure Composer

To automatically apply the correct `.htaccess` file for the current environment at deployment time, add a Composer post-install script to your `composer.json` in your local code base:



```json
"scripts": {
  "post-install-cmd": [
    "@php -r \" define('HTSRC', 'web/.htaccess_' . getenv('ENVIRONMENT')); echo (file_exists(HTSRC) && copy(HTSRC, 'web/.htaccess')) ? HTSRC.' copied to web/.htaccess'.PHP_EOL : ''; \""
  ]
}
```

This Composer post install command will replace the contents of the `.htaccess` file with the contents of the file `.htaccess_production`, if that file is present and the environment variable `ENVIRONMENT` is set to `production`.

On fortrabbit, depending on build steps, usually each time you [deploy with Git](/dev/learn/git-push-deploy), composer install runs automatically.

You can extend the concept to have more stages of course (see our [multi staging article](/dev/learn/staging-environments)). To add custom rules for a staging environment, like http-auth - see [HTTP auth article](/dev/htaccess/http-auth) - create a matching `.htaccess_staging` file and set the `ENVIRONMENT` accordingly.

This also assumes that the root path of your App is `web`, which is true for Craft CMS. In the Composer example above, replace the `web` with the root path of your project.
