在RewriteCond中做什么双冒号? [英] What Double Colon does in RewriteCond?
本文介绍了在RewriteCond中做什么双冒号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例:
RewriteCond %{REQUEST_URI}::$1 ^(.*?)/?(.*)::\2$
看起来像这个运算符在任何参考书或手册中都找不到.我在哪里可以找到它,或者任何人都可以解释该运算符的作用?
Looks like this operator is nowhere to find in any reference or manual. Where can I find it or anyone could explain what this operator does?
推荐答案
规则如下:
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
也可以写成(在条件的任何一侧使用##
作为固定的定界符):
can also be written as (using ##
as fixed delimiter on either side of condition):
RewriteCond %{REQUEST_URI}##$1 ^(.*?/)(.*)##\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
说明:
- 您可以使用从
RewriteCond
中的RewriteRule
捕获的$1
,因为mod_rewrite
实际上向后处理规则集.它以RewriteRule
中的模式开头,如果匹配,则继续检查一个或多个RewriteCond
. - 因此您可以在
RewriteCond
中看到,LHS(左侧/测试字符串)可以使用反向引用变量,例如$1
,$2
或%1
,%2
等,但是RHS(右侧),即条件字符串不能使用,这些$1
,$2
或%1
,%2
变量. - 在RHS条件部分中,我们只能使用的<反向引用>是内部反向引用,即我们在此条件本身中捕获的组.它们用
\1
,\2
等表示. - 在您的
RewriteCond
中,第一个捕获的组是(.*?/)
.它将由内部后向引用\1
表示. - 您可以指出,该规则基本上是通过比较
%{REQUEST_URI}
和$1
动态地找到RewriteBase
.%{REQUEST_URI}
的示例将是/directory/foobar.php
,而同一示例URI的$1
的示例将是foobar.php
.^(.*?/)::(.*)\1$
将差异放在第一个捕获的组%1
或\1
中.在这里,它将使用值/directory/
填充%1
或\1
,稍后将在设置环境变量%{ENV:BASE}
时使用该值,即E=BASE:%1
.
- You could use
$1
captured fromRewriteRule
in yourRewriteCond
becausemod_rewrite
actually processes a ruleset backwards. It starts with the pattern in theRewriteRule
, and if it matches, goes on to check the one or moreRewriteCond
. - So as you can see in a
RewriteCond
, the LHS (left-hand side / test string) can use backreference variables e.g.$1
,$2
OR%1
,%2
etc but RHS (right-hand side) i.e. condition string cannot use these$1
,$2
OR%1
,%2
variables. - Inside the RHS condition part only backreference we can use are internal back-references i.e. the groups we have captured in this condition itself. They are denoted by
\1
,\2
etc. - In your
RewriteCond
first captured group is(.*?/)
. It will be represented by internal back-reference\1
. - As you can mark out that this rule is basically finding
RewriteBase
dynamically by comparing%{REQUEST_URI}
and$1
. An example of%{REQUEST_URI}
will be/directory/foobar.php
and example of$1
for same example URI will befoobar.php
.^(.*?/)::(.*)\1$
is putting the difference in 1st captured group%1
or\1
. Here it will populate%1
or\1
with the value/directory/
which is used later in setting up env variable%{ENV:BASE}
i.e.E=BASE:%1
.
这篇关于在RewriteCond中做什么双冒号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文