.htaccess 中的动态环境变量

Dynamic environment variable in .htaccess

我正在努力找出如何根据我的场景使用 SetEnvIf 让环境变量工作,并且想知道是否有人可以告诉我该怎么做,或者举个例子。

我的结果是我需要以下重定向以根据环境变量触发,我有其他情况也需要使用此逻辑。

我有以下内容,所以重定向只发生在生产环境中

<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

理想情况下我的伪代码是

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=[=10=]
SetEnvIf environment ^(.*)$ environment=production
SetEnvIf Host ^staging. environment=staging
SetEnvIf Host .dev$ environment=dev
SetEnvIf env_defined .+ environment=[=10=]
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}/ [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>