phpapache.htaccessmod-rewriteacquia

.htaccess AuthUserFile variable based on environment


I'm trying to load the .htpasswd file based on environment. If I hard code the URL it works fine.

AuthType Basic
AuthName "Password Protected"
AuthUserFile /var/www/html/sitename.dev/docroot/.htpasswd
Require valid-user

Our hosting provider is providing a global variable you can use in htaccess.

%{ENV:AH_SITE_ENVIRONMENT} The Acquia hosting environment — possible values are dev, test, or prod

Using the variable we can check on which environment we currently are. If needed it can also be checked with the following rule: SetEnvIf Host ^dev\. DEV but I would like to prevent that.

I wanted to use an IfDefine but ifdefine doesn't read global variables. Is it possible to do one of the following?

  1. Make the path a variable and store the path with the AH_SITE_ENVIRONMENT variable and then do something like this: AuthUserFile MyConfiguredPathVar
  2. Put a var in the AuthUserFile directly to concat like so AuthUserFile /var/www/html/sitename.%{ENV:AH_SITE_ENVIRONMENT}/docroot/.htpasswd
  3. Any other way to use ifdefine and check if AH_SITE_ENVIRONMENT = dev

Apache version 2.2.22

UPDATE 1: It's possible to check the env with RewiteCond

# Determine whether environment is production:
RewriteCond %{ENV:AH_SITE_ENVIRONMENT} !prod

Solution

  • The problem here is auth modules don't have an access to ENV variables due to they are loaded before mod_env and setenvif_module modules. It means SetEnv and SetEnvIf directives don't work during auth processing and variables have not been set up yet.

    You can check the order of loaded module by httpd -M