Search code examples
regexapache.htaccessmod-rewrite

Single RewriteRule to create a redirection with two conditions


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]
    

Solution

  • 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]