.htaccess中的动态环境变量



我很难弄清楚如何根据我的场景使用SetEnvIf让环境变量发挥作用,我想知道是否有人能告诉我如何做到这一点,或者举个例子。

我的结果是,我需要以下重定向到基于环境变量的fire,我还有其他情况也需要使用这种逻辑。

我有下面的,所以重定向只发生在生产上

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{ENV:environment} production
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

我希望我能设置以下内容,但无法实现。

SetEnvIf environment ^(.*)$ environment=production #set it to production, if it is not already set?
SetEnvIf Host ^staging. environment=staging
SetEnvIf Host .dev$ environment=wamp

理想情况下,我的psudo代码应该是

SetEnvIf environment is not set, set to production
SetEnvElse host starts with staging, set to staging
SetEnvElse host ends with dev, set to wamp

然后在我的PHP中,我得到了

<?php
if( getenv('environment') ){
    exit(getenv('environment'));
}
else {
    exit("environment not found");
}

我的输出肯定是

environment not found

我正在访问owen.jekyll-test.dev

有人能告诉我我做错了什么吗?

几个小时后,我终于找到了一个可行的解决方案,它允许基于动态.htaccess的脱离环境

对于任何对类似设置感兴趣的人来说,这里是我们的配置,它可以自动处理SSL、WWW重定向、Apache Auth和is_Admin标志。

# ----------------------------------------------------------------------
# | Admin Vars                                                       |
# ----------------------------------------------------------------------
SetEnvIf Remote_Addr ^43.432.136.23 is_admin=1 # Virgin Media
SetEnvIf Remote_Addr ^81.43.184.70 is_admin=1   # BT
SetEnvIf Remote_Addr ^164.23.234.6 is_admin=1    # Orbtalk
SetEnv office_ip 132.39.322.23
# ----------------------------------------------------------------------
# | Environment Detection                                               |
# ----------------------------------------------------------------------
SetEnvIf environment .+ env_defined=$0
SetEnvIf environment ^(.*)$ environment=production
SetEnvIf Host ^staging. environment=staging
SetEnvIf Host .dev$ environment=dev
SetEnvIf env_defined .+ environment=$0
SetEnvIf prefix ^(.*)$ prefix=www.
SetEnvIf Host ^www !prefix
# ----------------------------------------------------------------------
# | Password Protection                                                |
# ----------------------------------------------------------------------
AuthType Basic
AuthName "Protected Login"
AuthUserFile /var/sites/website/.htpasswd
Order deny,allow
Deny from all
Satisfy any
SetEnvIf environment "dev" allow
SetEnvIf environment "production" allow
Allow from env=is_admin
Allow from env=allow
Allow from env=noauth
Require valid-user
Require user my_username
# ----------------------------------------------------------------------
# | Forcing `https://`                                                 |
# ----------------------------------------------------------------------
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} !=on [OR]
    RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
    RewriteCond %{ENV:environment} production
    RewriteRule ^(.*)$ https://%{ENV:prefix}%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# ----------------------------------------------------------------------
# | Forcing the `www.` at the beginning of URLs                        |
# ----------------------------------------------------------------------
#
# NOTE: IF THE WEBSITE USES SSL, YOU'LL NEED TO MODIFY THE REWRITE URL LOCATION AND MODIFY THE CONDITION
#
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} !=on [OR]
    RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"' [OR]
    RewriteCond %{HTTP_HOST} !^www. [NC]
    RewriteCond %{ENV:environment} production
    RewriteRule ^ https://%{ENV:prefix}%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

最新更新