这个正则表达式/重写有什么作用?
在下面的代码中,正则表达式的作用是什么?
RewriteCond %{REQUEST_URI} ^/(?:[A-Za-z]+(?:/[A-Za-z]*)*)(?:/\d+)?$
RewriteRule .* var/www/%1/index.php [L]
重写规则的结果是什么?
谢谢,我正在帮助一个朋友修复一个旧的 CMS,我不知道为什么制造它的人会这样做。
In the following code, what does the regex do?
RewriteCond %{REQUEST_URI} ^/(?:[A-Za-z]+(?:/[A-Za-z]*)*)(?:/\d+)?$
RewriteRule .* var/www/%1/index.php [L]
What is the outcome of the re-wrtie rule?
Thanks, I am helping a friend fix an old CMS and I don't know why whomever made it did this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AFAIK 它会在你不期望的时候生成 404。为什么?
(?: Something) 括号与 () 基本相同,只是它们不设置匹配变量。由于所有括号都是这样的
%1
永远不会被定义。获取 Alexander 的描述
/letters{/letters}([/digits])
,其中/digits
是可选的。重复组是“零个或多个字母”,因此会吃掉任何/
字符,这些是贪婪匹配,因此/aa/bb/cc/123
将被解析为 (/aa
)(/bb
)(/cc
)(/
)(123
)。这将会失败,因为 123 与(?:/\d+)?
不匹配
(?:/\d+)?
因此它只会匹配 alpha 路径字符串,例如 /aaa/bbb/ccc 和%1 将是“”,在这些情况下,这将重定向到相对于当前路径的
var/www//index.php
,这可能是 DOCROOT,所以除非DOCROOT/var/www/index.php
存在,这将导致 404。AFAIK it will generate 404s when you don't expect it to. Why?
The (?: something) brackets are basically the same as () except that they don't set a match variable. Since all bracketing is like this
%1
will never be defined.Picking up Alexander's description
/letters{/letters}([/digits])
where the/digits
are optional. The repeat group is "zero or more letters" and hence will eat any/
characters, and these are greedy matches, so/aa/bb/cc/123
will be parsed as (/aa
)(/bb
)(/cc
)(/
)(123
). This will fail since 123 does not match(?:/\d+)?
So it will only match alpha path strings, e.g. /aaa/bbb/ccc and %1 will be "" and in these circumstances this will redirect to
var/www//index.php
relative to the current path, which will probably be the DOCROOT, so unlessDOCROOT/var/www/index.php
exists, this will result in a 404.它应该将
/letters{/letters}[/digits]
形式的所有请求重定向到本地文件var/www/SOMETHING/index.php
但它确实如此不是:首先,没有捕获组(所有组都有前导?:
,因此,不清楚SOMETHING
是什么)。接下来,var/www...
部分可能应该有一个前导/
。It is supposed to redirect all requests of the form
/letters{/letters}[/digits]
to a local filevar/www/SOMETHING/index.php
but it does not: first, there is no capturing group (all groups have leading?:
and, consequently, it is not clear whatSOMETHING
will be). Next, thevar/www...
part should perhaps have a leading/
.按照 mod-rewrite 帮助,
%1 应该引用最后一个 RewriteCond 中正则表达式的第一个捕获组。 但是正则表达式只有非捕获组...
Following the mod-rewrite help, the
%1
should refer to the first capturing group of the regexp from the last RewriteCond. But the regexp only has non-capturing groups...