我怎样才能有条件的 .htaccess 块? [英] How Can I Have A Conditional .htaccess Block?

查看:41
本文介绍了我怎样才能有条件的 .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>

这篇关于我怎样才能有条件的 .htaccess 块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆