.htaccess AuthUserFile variable based on environment

天大地大妈咪最大 提交于 2020-05-15 09:34:09

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/39891783/htaccess-authuserfile-variable-based-on-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!