apache 如何使用带有 URL 参数的 SetEnv

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1280220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:04:06  来源:igfitidea点击:

How to use SetEnv with a URL parameter

apache.htaccessmod-rewritecookiesapache2

提问by l0b0

I'm trying to implement language switching in .htaccess, and the only thing left now is to handle clients which don't support cookies. To do that, I must set prefer-languagewhen the user clicks a link with a languageparameter.

我正在尝试在 .htaccess 中实现语言切换,现在唯一剩下的就是处理不支持 cookie 的客户端。为此,我必须设置prefer-language用户单击带有language参数的链接的时间。

RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} (?:^|&)language=(en|fr|no)
RewriteRule ^(.*)$ ? [cookie=language:%1:.example.com,env=language:%1,R]

SetEnv prefer-language $language

The problem is with the last line - The value is always set to empty. It works if I hardcode it, but not if I try to refer to a variable. Is there some special syntax to refer to environment variables in this context, or is there some other way to set prefer-language?

问题出在最后一行 - 该值始终设置为空。如果我对它进行硬编码,它会起作用,但如果我尝试引用一个变量,它就会起作用。在此上下文中是否有一些特殊的语法来引用环境变量,或者是否有其他方法可以设置prefer-language

Edit: Cross-posted to Apache users list.

编辑:交叉发布到Apache 用户列表

回答by Gumbo

You can set environment variables with mod_rewrite as well. Actually, you already did that (see env/Eflag).

您也可以使用 mod_rewrite 设置环境变量。实际上,您已经这样做了(请参阅env/ Eflag)。



I can't test it with mod_negotiation myself, but the following should work and set the prefer-language:

我自己不能用 mod_negotiation 测试它,但以下应该可以工作并设置prefer-language

RewriteCond %{QUERY_STRING} ^((?:[^&]&)*)language=(en|fr|no)&?([^&].*)?$
RewriteRule ^ %{REQUEST_URI}?%1%3 [L,CO=language:%2,R]
RewriteCond %{HTTP_COOKIE} (^|[,\s])language=([^\s,;]+)
RewriteRule ^ - [L,E=prefer-language:%2]
SetEnvIf REDIRECT_prefer-language (.+) prefer-language=

But it would be far easier if you put the language identifier into the URL path like /en/…:

但是,如果您将语言标识符放入 URL 路径中,则会容易得多,例如/en/…

SetEnvIf Request_URI ^/(en|fr|no)/ prefer-language=
SetEnvIf REDIRECT_prefer-language (.+) prefer-language=

I don't know if you need the additional/second SetEnvIfvariable.

我不知道您是否需要附加/第二个SetEnvIf变量。

回答by l0b0

Looks like there's no support for variables in SetEnv, but here's a working configuration if someone else is trying to do the same. It's a simpler kind of language selection, since it just copies the languageparameter from the referer to the current URL if it's not changed:

看起来 SetEnv 中不支持变量,但如果其他人试图做同样的事情,这里有一个工作配置。这是一种更简单的语言选择,因为它只是language将引用中的参数复制到当前 URL(如果它没有更改):

RewriteEngine On
RewriteBase /

# Keep the language parameter if specified in the last URL
RewriteCond %{HTTP_REFERER} ^(?:.*[&?])?language=(en|fr|no).*$
RewriteCond %{QUERY_STRING} !^(?:.*&)?language=(en|fr|no).*$
RewriteRule ^(.*)$ ?language=%1 [redirect=permanent]

# Set the language from the URL parameter
RewriteCond %{QUERY_STRING} ^(?:.*&)?language=(en|fr|no).*$
RewriteRule ^ - [env=prefer-language:%1]

# Cache only when the language parameter is set
<IfDefine !prefer-language>
    Header set Vary *
</IfDefine>