skip to Main Content

I found this rule which works fine with just one condition which is: foobar is in the string.

I need to change this to include a new condition to have two conditions (instead of one):

  • foobar is in the string. This is already working.

  • meetball is NOT in the string.

    RewriteRule ^(.*)foobar(.*)$ http://www.example.com/index.php [L,R=301]
    

2

Answers


  1. Please try following, written as per your shown samples. Also you need to create groups (.*) since you are not using them while redirection. You could add NC flag of apache to enable ignorecase to the URI values.

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


    OR without negative lookahead try with usual condition check. Please make sure either you put above Rulesets OR following rulesets one at a time only.

    RewriteEngine ON
    RewriteRule %{REQUEST_URI} !metaball [NC]
    RewriteRule foobar http://www.example.com/index.php [NC,L,R=301]
    
    Login or Signup to reply.
  2. You can use negative lookahead pattern:

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

    (?!.*meetball) will fail the pattern match if meatball is found anywhere in URI. Also there is no need to use grouping hence (...) is removed in my answer.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search