问题描述
我正在尝试根据环境加载.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.
使用变量,我们可以检查当前所处的环境.如果需要,也可以使用以下规则进行检查: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?
- 将路径设为变量,并使用
AH_SITE_ENVIRONMENT
变量存储路径,然后执行以下操作:AuthUserFile MyConfiguredPathVar
- 像这样
AuthUserFile /var/www/html/sitename.%{ENV:AH_SITE_ENVIRONMENT}/docroot/.htpasswd
一样直接在AuthUserFile中放置一个变量以进行连接 - 使用ifdefine并检查
AH_SITE_ENVIRONMENT = dev
的其他方法
- Make the path a variable and store the path with the
AH_SITE_ENVIRONMENT
variable and then do something like this:AuthUserFile MyConfiguredPathVar
- Put a var in the AuthUserFile directly to concat like so
AuthUserFile /var/www/html/sitename.%{ENV:AH_SITE_ENVIRONMENT}/docroot/.htpasswd
- 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_env
和setenvif_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 byhttpd -M
这篇关于.htaccess基于环境的AuthUserFile变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!