What does $ 1 mean in htaccess?

I am viewing the symfony2 source code. In the htaccess file for your sample website, I found %{REQUEST_URI}::$1 as follows:

 RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$ RewriteRule ^(.*) - [E=BASE:%1] 

The comment above this rule explains

Next, all other front controller requests are overwritten. The condition ensures that if you use Apache aliases for mass shared hosting, a base path will be added to ensure the correct resolution of the app.php file; It will also work in non-smoothed environments, providing a safe, one-time solution.

However, this does not explain ::$1 or ::\2 .

Are they backlinks? If not, what are they? What is their purpose?

+6
source share
2 answers

In my Zend project, I came across almost the same htaccess file, and here are my thoughts and hope this helps.

The htaccess file (located in the Zend project directory, same as index.php) says

RewriteCond% {REQUEST_URI} :: $ 1 ^ (/.+) (. +) :: \ 2 $

RewriteRule ^ (. *) $ - [E = BASE:% 1]

RewriteRule ^ (. *) $% {ENV: BASE} index.php [NC, L]

Suppose Zend is installed at http://mydomain.com/zend (let it rename it later) and we request yourdomain / mycontroller / myaction

Therefore,% {REQUEST_URI} will be "/ zend / mycontroller / myaction".

Note that $ 1, which is a template in the RewriteRule directive in the context of htaccess [1], "will initially be mapped to the file path, after removing the prefix that led the server to the current RewriteRule (for example," app1 / index.html "or" index .html "depending on where the directives are defined).

Therefore, $ 1 will be "mycontroller / myaction".

And% {REQUEST_URI} :: $ 1 will be "/ zend / mycontroller / myaction :: mycontroller / myaction".

The above line will be matched with ^ (/.+) (. +) :: \ 2 $. Please note that for two gripping groups in parentheses, i.e. (/.+) (. +) to :: many combinations can match this. For instance:

Group 1: / z

Group 2: end / mycontroller / myaction

or

Group 1: / zend / mycontroller / myactio

Group 2: n

and everything in between is a valid coincidence. In fact, the most interesting will be

Group 1: / zend /

Group 2: mycontroller / myaction

which (is the only case) makes a backward link \ 2 (after: :) matches the second group.

In this case, "/ zend /" will be stored in the BASE environment variable, which is what the first RewriteRule does. % 1 refers to the first matched line in the RewriteCond, which is "/ zend /".

Looking at the second RewriteRule, it is clear why this is necessary. Since index.php can only be found in /zend/index.php, we need to add "/ zend /" before index.php.

Here we intend to use the URL path as a substitute for the second RewriteRule directive. Refer to [1] and find the "relative path of the DocumentRoot to the resource that will be served" in the "RewriteRule Directive" section.

All of the above leaves the query string unchanged / untouched. It depends on index.php how to parse the query string (as well as the URI).

Finally, there comes the case when Zend is installed in the root of the domain.

% {REQUEST_URI} will be "/ mycontroller / myaction". $ 1 will be "mycontroller / myaction".

The line that will match the RewriteCond will be "/ mycontroller / myaction :: mycontroller / myaction".

This time, the second group in (/.+)(.+) will never match "mycontroller / myaction" since there must be at least one letter after the initial backslash for the first group, which makes the second group closed as "ycontroller / myaction ", but not exactly" mycontroller / myaction ", so there can be no match.

As a result, the first RewriteRule is not used. The BASE enviornment variable will not be set, and when the second RewriteRule uses it, it will simply be empty.

References

[1] http://httpd.apache.org/docs/current/mod/mod_rewrite.html

+10
source

$1 in %{REQUEST_URI}::$1 refers to the matched line of the RewriteRule directive, i.e. matched string .* to ^(.*) . Thus, %{REQUEST_URI}::$1 expands to the requested URI path provided by the user, and the current internal path and URI request, separated by :: .

The pattern ^(/.+)(.+)::\2$ used to search for a prefix (the first capture group), which forces the rest to match the part for :: ( \2 is a backward link to the matched line of the second capture group template).

If such a match is found, the prefix is ​​stored in the BASE environment variable ( [E=BASE:%1] , where %1 refers to the matching string of the previous successful match to the RewriteCond pattern).

+4
source

Source: https://habr.com/ru/post/945883/


All Articles