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 aredev
,test
, orprod
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?
AH_SITE_ENVIRONMENT
variable and then do something like this: AuthUserFile MyConfiguredPathVar
AuthUserFile /var/www/html/sitename.%{ENV:AH_SITE_ENVIRONMENT}/docroot/.htpasswd
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
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