mod_rewrite 从 URI 开始匹配

mod_rewrite match from start of URI

下午好, 我很难让我的 mod_rewrite 规则从 URI 的开头匹配。我已经阅读了手册,但我一定遗漏了一些(可能很明显的)语法巫术。请考虑以下事项:

RewriteCond %{REQUEST_URI}              ^/(.*)/(foo|bar)/(.*) 
RewriteRule ^(.*)/(.*)/(.*)/            destination.php?first=&second=&third=              [QSA,L]

如果第二个 URI 参数是 foo 或 bar,(例如 url。com/first/foo/third)这将成功重定向到目标,并带有相关参数。

如果 foo 或 bar 出现在第三个参数中(例如 url.com/first/second/foo),我希望发生不同的重定向。然而,下面的规则被忽略,上面的规则仍然优先

RewriteCond %{REQUEST_URI}              ^/(.*)/(something-else)/(.*) 
RewriteRule ^(.*)/(.*)/(.*)/            destination.php?first=&second=&third=              [QSA,L]

我想我一定是错过了一个明显的方法来强制重写只匹配 URI 的开头 - 我已经尝试在前面加上斜杠来强制它到根级别,但到目前为止没有任何快乐.

如有任何帮助,我们将不胜感激! 谢谢

编辑 事实证明,这是由于一个基本的 RegEx 错误,而不是我错误地假设匹配需要从 URI 的开头开始。我不会编辑标题,因为其他人可能会犯类似的错误。

.*太贪心了。试试这 2 条规则:

RewriteRule ^([^/]+)/(foo|bar)/([^/]+)/?$ destination.php?first=&second=&third= [QSA,L]

RewriteRule ^([^/]+)/([^/]+)/(foo|bar)/?$ destination2.php?first=&second=&third= [QSA,L]