问题
Example:
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?
回答1:
Rules like this:
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]
Explanation:
- 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
.
来源:https://stackoverflow.com/questions/37605218/what-double-colon-does-in-rewritecond