我怎样才能有一个有条件的 .htaccess 块?

问题描述:

这是您以前可能遇到过的 Apache 问题.我想要一个源包,我可以将它部署到我的工作站、我的临时服务器和我的生产服务器,但是它可以根据 URL 加载不同的 .htaccess 设置.

This is an Apache question you've probably come across before. I want to have one source package that I can deploy to my workstation, my staging server, and my production server, but for it to load different .htaccess settings based on what the URL was.

请注意,我使用了带有 IfModule 调用的 kludge,但这不适用于我们的新生产服务器,因为它与我的临时服务器共享所有相同的模块.

Note that I was using a kludge with an IfModule call, but that won't work with our new production server because it shares all the same modules as my staging server.

注意我需要将 SetEnv 与这些重写捆绑在一起.目前,如果您使用 RewriteCond,它只与下面的 RewriteRule 相关联,而不与下面的 SetEnv 相关联.

Note I need to bundle SetEnv with these rewrites. Currently if you use RewriteCond, it only ties to the following RewriteRule, but not the SetEnv underneath.

不使用 SetEnv,而是使用 RewriteRule 本身的环境变量设置能力:

Instead of using SetEnv, use the environment variable setting capabilities of RewriteRule itself:

RewriteCond %{HTTP_HOST} =foo.com
RewriteRule ^ - [E=VARNAME:foo]

RewriteCond %{HTTP_HOST} =bar.com
RewriteRule ^ - [E=VARNAME:bar]

虽然我更喜欢通过在启动时将标志传递给 httpd 进程并使用 IfDefine 块查找它们来做这种事情.

Although I prefer doing this sort of thing by passing flags to the httpd process at startup and looking for them using IfDefine blocks.

<IfDefine FOO>
SetEnv VARNAME foo
</IfDefine>

<IfDefine BAR>
SetEnv VARNAME bar
</IfDefine>