.htaccess 测试 ENV 是否为 true 或已定义

发布于 2024-12-15 06:08:41 字数 284 浏览 2 评论 0原文

如果 ENV 为 true 或已定义(如果未定义,则假定为 false),我需要运行一组 RewriteCond 语句。我一直在尝试使用:

RewriteCond %{ENV:IS_MOBILE} ^true$
RewriteCond ...

和(不确定这是否有效)

RewriteCond %{ENV:IS_MOBILE} !=true
RewriteCond...

但似乎都不起作用。我有什么办法可以做到这一点吗?

I need to run a set of RewriteCond statements if an ENV is true or defined (if not defined, then presumed false). I've been trying to use:

RewriteCond %{ENV:IS_MOBILE} ^true$
RewriteCond ...

and (not sure if this is even valid)

RewriteCond %{ENV:IS_MOBILE} !=true
RewriteCond...

But neither seem to work. Any way I can do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月牙弯弯 2024-12-22 06:08:41

这是一个现实生活中的示例,应该可以帮助您找到解决方案:

如果主机以“s”或“static”开头,则创建一个名为“STATIC”的环境变量并将其设置为“1”:

RewriteCond %{HTTP_HOST} ^(s|static)\.mysite\.com$
RewriteRule (.*) - [QSA,E=STATIC:1]

那么您可能会寻找:如果我的主机是“静态”,那么如果文件被认为是“静态”(img 等),则停止:

RewriteCond %{ENV:STATIC} 1
RewriteRule (.*)\.(pdf|jpg|jpeg|gif|png|ico)$ - [L]

现在,如果您想确保该变量不存在,只需检查它是否为空。这是我在上一条之后的规则:它检查变量是否不为空,如果是,则出现错误= 404(模拟“未找到”):

RewriteCond %{ENV:STATIC} !^$
RewriteRule (.*) /404.php [L]

RewriteCond %{ENV:IS_MOBILE} !^$
RewriteCond...

在您的情况下,如果您的环境不为空,您认为这是真的 如果您的环境等于您的情况,完全等于字符串 true,您认为它是“true”:

RewriteCond %{ENV:IS_MOBILE} =true
RewriteCond...

希望这会有所帮助。

Here's a real life sample that should help you to find the solution:

If the host begins by "s" or "static" then create an environment variable called "STATIC" and set it to "1":

RewriteCond %{HTTP_HOST} ^(s|static)\.mysite\.com$
RewriteRule (.*) - [QSA,E=STATIC:1]

Then what you may look for: if my hosts is "static" then if the files are considered "static" (img and so on) then stop:

RewriteCond %{ENV:STATIC} 1
RewriteRule (.*)\.(pdf|jpg|jpeg|gif|png|ico)$ - [L]

Now if you want to be sure that the variable doesn't exist, just check if it's empty. Here's my rule just after the previous: it checks if the variable is not empty, and if so, there's an error = 404 (to simulate "not found"):

RewriteCond %{ENV:STATIC} !^$
RewriteRule (.*) /404.php [L]

In your case if your env is not empty you suppose it's true:

RewriteCond %{ENV:IS_MOBILE} !^$
RewriteCond...

In your case if your env is equals, exactly equals to the string true you suppose it's "true":

RewriteCond %{ENV:IS_MOBILE} =true
RewriteCond...

Hope this helps.

∞觅青森が 2024-12-22 06:08:41

我认为您需要首先使用 IfDefine 包装代码

<IfDefine %{ENV:IS_MOBILE}>
     RewriteCond %{ENV:IS_MOBILE} true
</IfDefine>

I think you need to wrap the code using IfDefine first

<IfDefine %{ENV:IS_MOBILE}>
     RewriteCond %{ENV:IS_MOBILE} true
</IfDefine>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文