是什么:: $ 1一个htaccess是什么意思? [英] What does ::$1 mean in an htaccess?

查看:366
本文介绍了是什么:: $ 1一个htaccess是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览Symfony2的框架来源。在他们为榜样网站htaccess文件,我找到了%{REQUEST_URI} :: $ 1 写法如下:

I've been browsing the symfony2 framework source. In the htaccess file for their example website, I found the %{REQUEST_URI}::$1 written as follows:

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

这上面的规则的注释说明

The comment above that rule explains

以下重写所有其他查询到前端控制器。该条件保证了,如果你是使用Apache的别名做好群众虚拟主机,基本路径将是prepended让app.php文件的妥善解决;它会在不走样的环境中工作,以及,提供一个安全,一个尺寸适合所有的解决方案。

The following rewrites all other queries to the front controller. The condition ensures that if you are using Apache aliases to do mass virtual hosting, the base path will be prepended to allow proper resolution of the app.php file; it will work in non-aliased environments as well, providing a safe, one-size fits all solution.

不过,这并不能说明 :: $ 1 :: \\ 2

However, that doesn't explain the ::$1 or ::\2.

他们是反向引用?如果不是,它们是什么?什么是他们的目的是什么?

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

推荐答案

我也遇到过几乎相同的htaccess文件在我的Zend项目,这里有我的想法,并希望它能帮助。

I have encountered almost the same htaccess file in my Zend project, and here are my thoughts and hope it helps.

该htaccess文件(位于Zend的项目目录,一样的index.php)说:

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

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

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

重写规则^ $ - [E = BASE:%1](。*)

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

重写规则^(。*)$%{ENV:BASE}的index.php [NC,L]

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

假设的Zend安装在 http://mydomain.com/zend (姑且称之为YOURDOMAIN以后)
我们正在请求YOURDOMAIN / myController的/ myaction

Suppose Zend is installed at http://mydomain.com/zend (let's call it yourdomain later on) and we are requesting yourdomain/mycontroller/myaction

因此%{REQUEST_URI}将是/的Zend / myController的/ myaction。

注意$ 1,它是在htaccess的上下文[1],最初将针对文件系统路径匹配RewriteRule指令的​​图案,除去preFIX导致服务器的当前重写规则后(如APP1 / index.html的或的index.html这取决于指令被定义在哪里)。

Note that $1, which is the pattern in the RewriteRule directive in the htaccess context [1], "will initially be matched against the filesystem path, after removing the prefix that led the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined)".

因此$ 1日将myController的/ myaction。

和%{REQUEST_URI} :: $ 1日将/的Zend / myController的/ myaction :: myController的/ myaction。

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

上面的字符串将根据^(/.+)(+):: \\ $ 2相匹配。请注意,在一轮大括号两个捕获组,即(/.+)(.+)前::多种组合可以匹配。例如:

The above string will be matched against ^(/.+)(.+)::\2$. Note that for the two capturing groups in round braces i.e., (/.+)(.+) before :: many combinations can match that. For example:

第1组:/ Z

第2组:年底/ myController的/ myaction

Group 2: end/mycontroller/myaction

第1组:/的Zend / myController的/ myactio

Group 1: /zend/mycontroller/myactio

第2组:N

和任何之间的有效匹配。实际上,最有趣的是

and anything in between is a valid match. In fact, the most interesting one would be

第1组:/的Zend /

Group 1: /zend/

2组:myController的/ myaction

Group 2: mycontroller/myaction

其中(是唯一的情况下),使反向引用\\ 2(后::)第二组匹配

which (is the only case that) makes backreference \2 (after ::) to the second group a match.

在这种情况下,/ Zend的/将被存储在环境变量BASE其为第一重写规则做什么。 %1指的RewriteCond第一个匹配的字符串是/ zend的/\".

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

寻找在第二重写规则,很显然这是为什么有需要此。作为index.php文件只能在/zend/index.php可以发现,我们需要添加/的Zend /中的index.php的面前。

Looking at the second RewriteRule, it is clear that why there is a need for this. As index.php can only be found in /zend/index.php, we need to add "/zend/" in front of index.php.

在这里,我们假设使用的URL路径作为替换为第二个RewriteRule指令。请参阅[1],并搜索A DocumentRoot的相对路径要服务的资源下的RewriteRule指令部分。

Here we assume to use the URL-path as Substitution for the second RewriteRule directive. Refer to [1] and search for "A DocumentRoot-relative path to the resource to be served" under the RewriteRule Directive section.

以上全部离开查询字符串不变/不变。它是由到index.php如何解析查询字符串(以及该URI)。

All the above leave the query string unchanged/untouched. It is up to index.php how to parse the query string (as well as the URI).

最后去的地方的Zend安装在域根的情况。

%{REQUEST_URI}将是/ myController的/ myaction。
$ 1日将myController的/ myaction。

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

该字符串由RewriteCond指令的匹配将是/ myController的/ myaction :: myController的/ myaction。

The string to be matched by RewriteCond will be "/mycontroller/myaction::mycontroller/myaction".

这一次(/.+)(.+)因为需要有至少一个字母以下为第一组最初的反斜线永远比不上myController的/ myaction,使得第二组接近第二组作为ycontroller / myaction但不完全myController的/ myaction,所以不可能有一个匹配。

This time the second group in (/.+)(.+) will never match "mycontroller/myaction" as there needs to be at least one letter following the initial backslash for the first group, making the second group as close as "ycontroller/myaction" but not exactly "mycontroller/myaction" so there cannot be a match.

其结果是,不使用所述第一重写规则。在BASE enviornment变量将不会被设置,并且当所述第二重写规则使用它,它会简单地是空的。

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.

参考

[1] <一个href=\"http://httpd.apache.org/docs/current/mod/mod_rewrite.html\">http://httpd.apache.org/docs/current/mod/mod_rewrite.html

这篇关于是什么:: $ 1一个htaccess是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆