.htaccess基于环境的AuthUserFile变量 [英] .htaccess AuthUserFile variable based on environment

查看:118
本文介绍了.htaccess基于环境的AuthUserFile变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据环境加载.htpasswd文件.如果我对URL进行硬编码,则可以正常工作.

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

我们的托管服务提供商提供您可以在其中使用的全局变量htaccess.

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

%{ENV:AH_SITE_ENVIRONMENT} Acquia托管环境-可能的值为devtestprod

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

使用变量,我们可以检查当前所处的环境.如果需要,也可以使用以下规则进行检查:SetEnvIf Host ^dev\. DEV,但我想防止这种情况.

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.

我想使用IfDefine,但是ifdefine不读取全局变量.是否可以执行以下任一操作?

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

  1. 将路径设为变量,并使用AH_SITE_ENVIRONMENT变量存储路径,然后执行以下操作:AuthUserFile MyConfiguredPathVar
  2. 像这样AuthUserFile /var/www/html/sitename.%{ENV:AH_SITE_ENVIRONMENT}/docroot/.htpasswd
  3. 一样直接在AuthUserFile中放置一个变量以进行连接
  4. 使用ifdefine并检查AH_SITE_ENVIRONMENT = dev
  5. 的其他方法
  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 2.2.22版

更新1:可以使用RewiteCond

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

推荐答案

此处的问题是auth模块无法访问ENV变量,因为它们是在mod_envsetenvif_module模块之前加载的.这意味着SetEnv和SetEnvIf指令在身份验证处理期间不起作用,并且尚未设置变量.

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.

您可以通过以下方式检查已加载模块的顺序 httpd -M

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

这篇关于.htaccess基于环境的AuthUserFile变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆