mod_rewrite 问题:否定
我试图更好地理解 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(?= ...)
和(?! ...)
是 Perl 正则表达式和 PCRE,这是 Apache 使用的正则表达式库。它们分别是正向和负向先行断言:如果空字符串后面的文本与括号中的内容匹配或不匹配,则它们匹配空字符串。它们是非捕获的,因此它们不定义任何
$n
(这是毫无意义的,因为它们匹配空字符串)。(?: ...)
也是非捕获的,它用于对子表达式进行分组。您的第一条规则应该在
.htaccess
中工作(但不能在虚拟主机配置文件中),尽管将其写为“也许另一个规则正在与它交互”会更正确。您可以使用
RewriteLog
和
RewriteLogLevel
。(?= ...)
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 asPerhaps another rule is interacting with it. You can check what exactly is being matched and rewritten with
RewriteLog
andRewriteLogLevel
.“!”表示否定的意思。就像 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