我需要的是这样的:
SetEnv foo bar.baz
RewriteEngine On
RewriteCond %{HTTP_HOST} ^%{foo}$
RewriteRule ^(.*)$ http://www.foo/$1 [L]
UPD:
我已经做如下:
SetEnv HOSTNAME_ENV testsite
RewriteEngine On
RewriteCond %{HTTP_HOST} ^%{ENV:HOSTNAME_ENV}
RewriteRule ^(.*)$ http://www.%{ENV:HOSTNAME_ENV}/$1 [L,R=301]
不过,这是行不通的。 %{ENV:HOSTNAME_ENV}没有定义
But it doesn't work. %{ENV:HOSTNAME_ENV} is not defined.
这是行不通的,因为的mod_rewrite
不在测试模式扩展变量。因此,你的前pression将尝试匹配正EX pression ^%{FOO} $
,不是 ^ value_of_foo $
。
This won't work, because mod_rewrite
does not expand variables in test patterns. Consequently, your expression will attempt to match the regular expression ^%{foo}$
, not ^value_of_foo$
.
此外,对于文档 SETENV
也使得特别注意有关订单的处理中关系的mod_rewrite
。这实际上不来这里发挥作用,尽管后来在请求处理比他们描述那里的情况下被应用的规则。如果不进行重写的测试对你设置环境变量之前导致内部重定向,这将是空白的。
Additionally, the documentation for SetEnv
also makes a special note about order-of-processing in relationship to mod_rewrite
. This actually does come into play here, despite the rules being applied later in the request handling than the scenario they're describing there. If you don't perform a rewrite that causes an internal redirection before testing against the environment variable you set, it will be blank.
假设你正确设置变量,一个潜在的解决方法是做到这一点,但我觉得它不是万无一失的:
Assuming you set the variable correctly, a potential workaround is to do this, although I feel like it isn't foolproof:
RewriteCond %{HTTP_HOST}#%{ENV:foo} ^([^#]+)#1$