What Double Colon does in RewriteCond?

牧云@^-^@ 提交于 2019-12-21 05:10:06

问题


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 from RewriteRule in your RewriteCond because mod_rewrite actually processes a ruleset backwards. It starts with the pattern in the RewriteRule, and if it matches, goes on to check the one or more RewriteCond.
  • 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 be foobar.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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!