Apache RewriteCond中变量的负比较 [英] Negative Comparison of Variables in Apache RewriteCond

查看:52
本文介绍了Apache RewriteCond中变量的负比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rewrite块,旨在将用户重定向到反映其语言环境的URL(如果还没有的话).这似乎需要我比较两个变量,我意识到这在RewriteCond中有点棘手,但是必须有一种优雅地做到这一点的方法,而不是拥有多个不同的规则.

I have a Rewrite block that aims to with redirect users to a URL that reflects their locale, if they aren't already. This seems to require me to compare two variables, which I realize is a little tricky in a RewriteCond, but there's gotta be a way to do this elegantly instead of having several different rules.

基本上-如果我在这里但不应该(由于地理围栏),请重定向到所标识的路径.

Basically - if I'm here but shouldn't be (because of geofencing), redirect to the identified path.

# 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]

我知道有关组合两个变量的语法,但是我在这里还没有得到否定的测试.

I know there's some syntax about combining two variables, but I haven't been able to get a negative test here.

推荐答案

您可以使用如下规则:

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}/ 
}

这篇关于Apache RewriteCond中变量的负比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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