本文介绍了使用 SetEnv 重写条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 .htaccess 中我设置:

SetEnv APPLICATION_ENV development

我想检查 APPLICATION_ENV 是否等于 development 然后运行 ​​app_dev.php,否则运行 app.php

And I want to check if APPLICATION_ENV equals development then run app_dev.php, otherwise app.php

SetEnv APPLICATION_ENV development

RewriteCond %{ENV:APPLICATION_ENV} development
RewriteRule .? %{ENV:BASE}/app_dev.php [L]
RewriteRule .? %{ENV:BASE}/app.php [L]

但是它不起作用 - 总是运行 app.php 脚本.我该如何解决?

However it does not work - always runs app.php script. How should I fix it ?

推荐答案

由于我不得不自己测试,只能确认你写的内容是正确的,我开始环顾四周,发现了这个post关于SetEnvSetEnvIfRewriteRule 可见性.看起来 SetEnvRewriteCond 不可见,如果我将您的示例更改为使用:

Since I had to test this myself and could only confirm that what you wrote was correct, I started to look around and found this post regarding SetEnv, SetEnvIf and RewriteRule visibility. It looks like SetEnv is not visible for RewriteCond and if I change your example to use:

SetEnvIf APPLICATION_ENV  ^(.*)$ APPLICATION_ENV=development

我实际上了解您必须加载app_dev.php 的规则.您也可以使用 RewriteRule 设置变量:

I actually get the rules you have to load app_dev.php. You could set the variable using RewriteRule as well:

RewriteRule .* - [E=APPLICATION_ENV:development,NE]

但是,看起来不能使用 SetEnv(在 Apache 2.2.22 上测试).

However, looks like SetEnv can't be used (Tested on Apache 2.2.22).

编辑
正如 julp 在对这篇文章的评论中指出的,这在 Apache 文档部分 Setting Environment Variables:

The SetEnv directive runs late during request processing meaning that directives 
such as SetEnvIf and RewriteCond will not see the variables set with it.

这篇关于使用 SetEnv 重写条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 18:20