Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Double Colon does in RewriteCond?

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?

like image 690
Felix Avatar asked Jun 03 '16 02:06

Felix


1 Answers

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.
like image 86
anubhava Avatar answered Oct 06 '22 04:10

anubhava