Backreferencing regex in apache -


i setting symfony framework system , configuring .htaccess file reroute requests when came across these 2 lines:

rewritecond %{request_uri}::$1 ^(/.+)/(.*)::\2$ rewriterule ^(.*) - [e=base:%1] 

this supposed give rewrite base proceeding rewrites. want know how $1 processed here since there no rewriterules preceding it? , clarification on how code works appreciated.

the $1 last rule capture group. it's little confusing because of way rules processed mod_rewrite. given rule:

rewritecond %{request_uri}::$1 ^(/.+)/(.*)::\2$ rewriterule ^(.*) - [e=base:%1] 

this mod_rewrite does:

  1. i have uri, let's apply rule
  2. i @ rewriterule line , checks pattern: uri matches ^(.*), ok good, let's continue
  3. now let's check conditions
  4. the %{request_uri}::$1 string gets mapped out uri , first capture group, happened in step 2
  5. the pattern in condition matches, apply target of rule
  6. the target -, pass uri through , apply flags

so rule gets "halfway" applied first, that's how $1 capture group gets set, conditions checked.

note if had:

rewritecond %{request_uri}::$1 ^(/.+)/(.*)::\2$ rewriterule ^.* - [e=base:%1] 

$1 blank because pattern in rule doesn't have capture group.

the \2 inline reference, references:

this----v ^(/.+)/(.*)::\2$ 

grouping.

an example of how condition works, have %{request_uri} of /abc/foo/bar.html, , rules in /abc/ directory. means condition string:

%{request_uri}::$1 

is

/abc/foo/bar.html::foo/bar.html 

and match takes part after :: , matches same thing before :::

this bit ----------v__________v /abc/foo/bar.html::foo/bar.html      ^-----------^____must match grouping 

and thus, left first grouping (/.+), /abc/. , have correct relative uri base, stored in environment variable "base".


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -