在.htaccess中使用自定义的环境变量 [英] Using custom environment variables in .htaccess

查看:435
本文介绍了在.htaccess中使用自定义的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的是这样的:

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$

这篇关于在.htaccess中使用自定义的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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