单个 RewriteRule 以创建具有两个条件的重定向

Single RewriteRule to create a redirection with two conditions

我发现这条规则只适用于一个条件:foobar 在字符串中。

我需要对此进行更改以包含一个新条件以具有两个条件(而不是一个):

请尝试按照您显示的示例编写。您还需要创建组 (.*) 因为您在重定向时没有使用它们。您可以添加 apache 的 NC 标志以对 URI 值启用忽略大小写。

RewriteEngine ON
RewriteRule ^(?!.*metaball).*foobar.*$ http://www.example.com/index.php [NC,L,R=301]


或者在没有否定前瞻的情况下尝试使用常规条件检查。请确保一次只放置一个以上规则集或以下规则集。

RewriteEngine ON
RewriteRule %{REQUEST_URI} !metaball [NC]
RewriteRule foobar http://www.example.com/index.php [NC,L,R=301]

您可以使用 negative lookahead pattern:

RewriteRule ^(?!.*meetball).*foobar http://www.example.com/index.php [L,R=301]
如果在 URI 中的任何地方找到 meatball

(?!.*meetball) 将使模式匹配失败。此外,不需要使用分组,因此 (...) 在我的回答中被删除。