Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex apacherewritecond中变量的负比较_Regex_Apache_Mod Rewrite - Fatal编程技术网

Regex apacherewritecond中变量的负比较

Regex apacherewritecond中变量的负比较,regex,apache,mod-rewrite,Regex,Apache,Mod Rewrite,我有一个重写块,目的是将用户重定向到一个反映他们所在地区的URL,如果他们还没有。这似乎需要我比较两个变量,我意识到这在一秒钟内有点棘手,但必须有一种方法来优雅地做这件事,而不是有几个不同的规则 基本上-如果我在这里但不应该在这里(因为地理围栏),重定向到已识别的路径 # Snip several SetEnvIf rules for the UserLocale variable used below RewriteCond %{REQUEST_URI} ^/(us|au|uk|eu)/$

我有一个重写块,目的是将用户重定向到一个反映他们所在地区的URL,如果他们还没有。这似乎需要我比较两个变量,我意识到这在一秒钟内有点棘手,但必须有一种方法来优雅地做这件事,而不是有几个不同的规则

基本上-如果我在这里但不应该在这里(因为地理围栏),重定向到已识别的路径

# Snip several SetEnvIf rules for the UserLocale variable used below

RewriteCond %{REQUEST_URI} ^/(us|au|uk|eu)/$
# This is where I'm having trouble. 
# This following condition should evaluate whether the requested URI matches the StoreLocale
RewriteCond %{ENV:UserLocale} !%1
RewriteRule ^(.*)$ https://example.com/%{ENV:UserLocale}/ [R=302,L]

我知道有一些语法,但我在这里没有得到否定测试。

您可以使用如下规则:

RewriteCond %{ENV:UserLocale}#$1 !^([^#]+)#\1$
RewriteRule ^(us|au|uk|eu)/$ https://example.com/%{ENV:UserLocale}/ [R=302,L,NC]
这将有效地满足本规范的要求:

if ( %{ENV:UserLocale} != $1 ) {
   RewriteRule ^(us|au|uk|eu)/$ https://example.com/%{ENV:UserLocale}/ 
}

所以当
%{ENV:StoreLocale}
不是
%{REQUEST\u URI}
时,应该触发
重写规则
?还有,
RewriteCond%{REQUEST_URI}^/(us | au | uk | eu)/$
背后的逻辑是什么?是的。本质上是:“如果请求的URL是
/(us | au | uk | eu)/
”,那么如果捕获的片段与定义的
StoreLocale
不匹配(例如,我请求的是/
eu
/但是StoreLocale是
us
),则重定向到包含StoreLocale的URL。(注意:将“StoreLocale”视为“UserLocale”可能更有上下文意义。我将更新)所以
%{ENV:UserLocale}
/us/
还是仅仅是
us
UserLocale
应该是'us','au','uk','eu'谢谢你,@anubhava!这是一种疯狂的正则表达式,你愿意解释一下吗?实际上没有那么疯狂。在LHS上我们结合了
%{ENV:UserLocale}
$1
值,用分隔符作为
。然后在RHS上,我们匹配
之前的部分并将其分组为组1。我们在
之后使用
\1
反向引用它,以确保在
之前和之后具有相同的值。
的存在否定了整个条件,从而使我们的行为生效f
%{ENV:UserLocale}!=$1
条件。