在.htaccess中声明variables以确定部署环境

我在.htaccess文件中定义了以下规则。

RewriteRule ^/home$ http://someothersite.com/home RewriteRule ^/dir1/dir2/$ http://someothersite.com/dir1/dir2/ 

我需要将主机名http://someothersite.com设置为一个.htaccessvariables。 此variables将根据部署环境位置而变化。 有没有可能做到这一点?

我必须要有3个部署环境:build,dev和stage。 对于每个环境,build中的.htaccess文件应该redirect到3个外部站点。

  • 构build:.htaccess文件应该redirect到build.someothersite.com
  • dev:.htaccess文件应该redirect到dev.someothersite.com
  • 阶段:.htaccess文件应该redirect到stage.someothersite.com

我不想为三种不同的环境创build3个不同的.htaccess文件,我只想创build一个.htaccess文件,而不用硬编码主机名:build.someothersite.com,dev.someothersite.com,stage.someothersite.com。

我需要这样的东西:

 RewriteRule ^/home.html =>%{ENV:external_host}/build.html 

是否可以在构build,开发或阶段环境中使用某些SetEnv指令在%{ENV:external_host}设置%{ENV:external_host}variables? 例如,该variables将设置如下所示:

  • 在dev中设置SetEnv = dev.someothersite.com,
  • SetEnv = build.someothersite.com
  • SetEnv = stage.someothersite.com分别在舞台上。

请记住,环境variables对于正在运行的用户的范围是本地的。 也就是说,$ home将parsing为Apache正在运行的用户的主文件夹。 即/ root或/ home / apache或/ sbin / nologin或其他版本。 所以,问题是…你想利用哪些variables?

你的问题没有多大意义。 看来你已经决定使用“variables”的解决scheme。 你应该说明实际的问题。 Apache非常灵活,有人可能有更好的方法来做到这一点。

尽pipe如此,通过mod_rewrite,您可以在RewriteCond中使用%{ENV:variable}访问环境variables,并使用SetEnv指令设置它们。 问题变成如何设置variables? 我不能回答这个问题,除非你说“你的位置”是什么意思。 如果“位置”是指虚拟主机,那么您将其设置为虚拟主机configuration。

另一种方法可能是使用RewriteMap。

这听起来像你想在Apache中使用条件variables。 我使用shell脚本在shell环境中有条件地设置variables,然后在重新启动时由Apacheinheritance。

您可以在Ubuntu上的/etc/apache2/envvars设置shell环境variables,FreeBSD上的/usr/local/etc/apache22/envvars.d/ 。 在CentOS / RHEL上,我认为正确的文件是/etc/sysconfig/httpd ,但是我不确定。

HTTPD启动脚本将在调用文件时获取文件。 这些variables成为Apache的shell环境的一部分,可以在Apacheconfiguration中引用。 你的条件逻辑可以像你想要的那样强大。

例如,我有类似于/usr/local/etc/apache22/envvars.d/hostname.env

 HOSTNAME=`hostname -s` if [ "$HOSTNAME" == "foo" ]; then export INCLUDE_CONFFILE = $HOSTNAME.conf else export INCLUDE_CONFFILE = default.conf done 

然后我在主要的httpd.conf中使用这个环境variables:

 # We have some host-specific settings for some hosts. # INCLUDE_CONFFILE is inherited from envvars.d/hostname.env Include etc/apache22/Includes/${INCLUDE_CONFFILE} 

这些variables也应该在.htaccess文件中可读。

请注意, 操作系统环境variables与Apache环境variables完全不同,所以不要混淆两者。 Apache HTTP Server手册说 :“虽然这些variables被称为环境variables,但它们与底层操作系统控制的环境variables不同。