.htaccess中的动态环境变量

Owe*_*wen 1 .htaccess setenvif

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

我的结果是我需要根据环境变量进行以下重定向,我还有其他需要使用此逻辑的情况.

我有下面的内容,所以重定向只发生在制作上

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

我希望我可以设置以下内容,但无法弄明白.

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

理想情况下,我的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
Run Code Online (Sandbox Code Playgroud)

然后在我的PHP中我得到了

<?php

if( getenv('environment') ){
    exit(getenv('environment'));
}
else {
    exit("environment not found");
}
Run Code Online (Sandbox Code Playgroud)

我的输出肯定是

environment not found
Run Code Online (Sandbox Code Playgroud)

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

任何人都能指出我做错的方向吗?

Owe*_*wen 7

经过几个小时,我终于得到了一个工作解决方案,允许基于环境的动态.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>
Run Code Online (Sandbox Code Playgroud)