mod_rewrite 问题:否定

发布于 2024-10-19 11:52:14 字数 646 浏览 0 评论 0原文

我试图更好地理解 mod_rewrite ,并且有一个特殊的问题,我认为我需要首先解决这个问题。

我正在使用以下

RewriteRule ^(?!index.php)(.*)$ index.php?path=$1 [NC,L]

等效重写 将 http://www.somesite.tld/a/b/c 重写为 index.php?path=a/b/c在这种情况下,如果

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [NC,L]
  • 没有RewriteCond,这将不起作用——path=index.php 将是结果,而无需专门忽略文件或说“不是index.php”。 这是为什么?
  • 另外,我有时看到的 ?!?: 语法是什么?我不明白 ? 当它没有任何前缀时的用法。
  • 为什么在上面的第一个 RewriteRule 中,第二对括号返回 $1 的匹配项?

干杯

I'm trying to understand mod_rewrite better and have one particular problem I think I need to get my head round first.

I am rewriting http://www.somesite.tld/a/b/c to index.php?path=a/b/c using the following

RewriteRule ^(?!index.php)(.*)$ index.php?path=$1 [NC,L]

An equivalent rewrite would, in this case, be

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [NC,L]
  • This does not work without the RewriteCond -- path=index.php would be the result without specifically ignoring files or saying 'not index.php'. Why is this?
  • Also, what is the ?! and ?: syntax that I sometimes see used? I do not understand the use of the ? when it is not prefixed by anything.
  • And why, in the first RewriteRule above, do the second pair of brackets return a match for $1?

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

卖梦商人 2024-10-26 11:52:14

(?= ...)(?! ...) 是 Perl 正则表达式和 PCRE,这是 Apache 使用的正则表达式库。它们分别是正向和负向先行断言:如果空字符串后面的文本与括号中的内容匹配或不匹配,则它们匹配空字符串。

它们是非捕获的,因此它们不定义任何 $n (这是毫无意义的,因为它们匹配空字符串)。 (?: ...) 也是非捕获的,它用于对子表达式进行分组。

您的第一条规则应该在 .htaccess 中工作(但不能在虚拟主机配置文件中),尽管将其写为“

RewriteRule ^(?!index\.php$)(.*)$ index.php?path=$1 [L]

也许另一个规则正在与它交互”会更正确。您可以使用 RewriteLogRewriteLogLevel

(?= ...) and (?! ...) is special syntax in Perl regular expressions and in PCRE, which is the regex library that Apache uses. They are, respectively, positive and negative lookahead assertions: they match an empty string if the text after it matches or does not match the content in the brackets.

They are non-capturing, so they don't define any $n (it would be pointless, since they match an empty string). (?: ...) is also non-capturing, it is used to group subexpressions.

Your first rule should work in .htaccess (but not in a virtual host configuration file), though it would be more correct to write it as

RewriteRule ^(?!index\.php$)(.*)$ index.php?path=$1 [L]

Perhaps another rule is interacting with it. You can check what exactly is being matched and rewritten with RewriteLog and RewriteLogLevel.

秋叶绚丽 2024-10-26 11:52:14

“!”表示否定的意思。就像 a = 1 (a 等于 1) a != 1 (a 不等于 1);
“f”表示文件。因此,如果与“!”一起使用,则“!-f”将表示“文件不存在”。下面的链接可能会更好地帮助您:

http://www.askapache.com/htaccess/htaccess.html
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
http://corz.org/serv/tricks/htaccess2.php

"!" means negation. Like a = 1 (a is equal one) a != 1 (a is not equal one);
"f" means file. So if you use together with "!", like "!-f" would be something "file does not exist". the links below may help you better:

http://www.askapache.com/htaccess/htaccess.html
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
http://corz.org/serv/tricks/htaccess2.php

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