Ravi Soni
Ravi Soni

Reputation: 2250

htaccess use OR condition in RewriteCond

I am looking for a solution where i can define the muliple match statement whitin the single RewriteCond statement,

The scenario is i want to redirect to home page if any of this string comes in the url: /it/, /de/, /fr/

I tried to wrote it like this:

  1. This is throwing SERVER ERROR:

    RewriteCond ^(/it/ | /de/ | /fr/ )
    RewriteRule 'myhomepageurl'
    
  2. I also tried this way too:

    RewriteCond /it/ [OR]
    RewriteCond /de/ [OR]
    RewriteCond /fr/ [OR]
    RewriteRule 'myhomepageurl'
    

This was giving unexpected results page loding infinite and admin was blocked. So i finaly took this way:

RewriteCond /it/
RewriteRule 'myhomepageurl'

RewriteCond /de/
RewriteRule 'myhomepageurl'

RewriteCond /fr/
RewriteRule 'myhomepageurl'

I am just curious about the single statement that can do this in single shot.

Thanks

Upvotes: 3

Views: 6116

Answers (2)

pjmorse
pjmorse

Reputation: 9294

Your first example is failing because of the spaces; a better regex would be something like

RewriteCond ^/(it|de|fr)/
RewriteRule 'myhomepageurl'

Either way, you should read When Not To Use Rewrite and use one of the simpler forms suggested there, e.g.

RedirectMatch /(it|de|fr)/ /myhomepageurl

Upvotes: 7

Ast Derek
Ast Derek

Reputation: 2729

Try this:

RewriteCond %{REQUEST_URI} /(en|de|it)/
RewriteRule .* myhomepageurl [R=301,L]

Upvotes: 3

Related Questions