.htaccess rewriteCond cascade multiple conditions with or / and

12 years ago, July 23, 2012
Reading time: 1 mins

Below is the code to cascade multiple conditions “rewriteCond” with or / and using .htaccess in apache server.

If you want AND conditions or in other words, If u are checking the conditions so that the redirect would be done only if all the conditions return true, following code snippet looks for two conditions,

 

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

 

The main point here to remember is if u want to redirect only if the two conditions are true they should be in two lines.

 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

 

If you want OR conditions or in other words, If u are checking the conditions so that the redirect would be done if any one of the conditions return true. In the following code snippet, the request_uri is compared with multiple patterns separated with | “OR”,

RewriteCond %{REQUEST_URI} ^/(old_dir|old_dir1|old_dir2).*$ [NC]
RewriteRule ^(.*)$ http://anilmaharjan.com.np/ [L,R=301]

Here the redirect will be done if any of the directory is tried to access namely, old_dir, old_dir1, old_dir2.

This is very helpful when you have changed your Websites CMS or framework and you want google bot to get redirected to your new pages.

Previous
Easiest way to change android bootscreen animation
Next
Firefox Icon not displayed for HTML files after Firefox Update
© 2024 Anil Maharjan