这个正则表达式/重写有什么作用?

发布于 2025-01-05 09:01:12 字数 258 浏览 1 评论 0原文

在下面的代码中,正则表达式的作用是什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花落人断肠 2025-01-12 09:01:12

重写规则的结果是什么?

AFAIK 它会在你不期望的时候生成 404。为什么?

  1. (?: Something) 括号与 () 基本相同,只是它们不设置匹配变量。由于所有括号都是这样的 %1 永远不会被定义。

  2. 获取 Alexander 的描述 /letters{/letters}([/digits]),其中 /digits 是可选的。重复组是“零个或多个字母”,因此会吃掉任何 / 字符,这些是贪婪匹配,因此 /aa/bb/cc/123 将被解析为 (/aa)(/bb)(/cc)(/ )(123)。这将会失败,因为 123(?:/\d+)?

    不匹配 (?:/\d+)?

  3. 因此它只会匹配 alpha 路径字符串,例如 /aaa/bbb/ccc 和%1 将是“”,在这些情况下,这将重定向到相对于当前路径的 var/www//index.php,这可能是 DOCROOT,所以除非DOCROOT/var/www/index.php 存在,这将导致 404。

What is the outcome of the re-write rule?

AFAIK it will generate 404s when you don't expect it to. Why?

  1. 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.

  2. 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+)?

  3. 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 unless DOCROOT/var/www/index.php exists, this will result in a 404.

小情绪 2025-01-12 09:01:12

它应该将 /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 file var/www/SOMETHING/index.php but it does not: first, there is no capturing group (all groups have leading ?: and, consequently, it is not clear what SOMETHING will be). Next, the var/www... part should perhaps have a leading /.

三寸金莲 2025-01-12 09:01:12

按照 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文