Dany Khalife
Dany Khalife

Reputation: 1850

HTaccess condition

I am trying to match certain rules Only when there is no "locale" in the query string

Here is how i am doing it:

RewriteCond %{QUERY_STRING} !locale
RewriteRule ^acceuil$ home.php?locale=fr [NC,L]
... (More rewrite rules)

Basically i want /acceuil to go to home.php?locale=fr (Working perfect)

Except that if somehow a link was like this /acceuil?locale=en i would like to make it go instead to /home which is equivalent to home.php?locale=en

Any chance to rewrite lets say "/acceuil" in the following way :

if(isset($locale)) {
      go to home.php?locale=$locale
}
else{
      go to home.php?locale=fr
}

Also i am wondering if the condition should be copy pasted before each rule ?

Please let me know if this is possible, and how i can get it going, if you have some useful reference for htaccess rewrites please share :)

Thanks

Upvotes: 0

Views: 497

Answers (2)

Olivier Pons
Olivier Pons

Reputation: 15778

Here's a generic rule that should work, and that makes sure the variable in the GET is locale (not localeee neither llllocale):

RewriteCond %{QUERY_STRING} (^|&)locale=([a-zA-A]+)(&|$)
RewriteRule ^acceuil$ home.php?locale=%1 [L,NC,QSA]

# if locale = en ...
RewriteCond %{QUERY_STRING} (^|&)locale=en(&|$)
# ... and it goes to home.php then go back to /home:
RewriteRule ^home.php$ /home [L,NC,QSA]

Note: if it's French then it's Accueil, not Acceuil :)

Upvotes: 1

undone
undone

Reputation: 7888

RewriteCond     %{QUERY_STRING}  !locale
RewriteRule     ^acceuil$               home.php?locale=fr  [L,NC]
RewriteCond     %{QUERY_STRING}  locale=([a-zA-Z]{2})

RewriteRule     ^acceuil$               home.php?locale=%1  [L,NC]

Edit:
First condistion just check the presence of local in query string. If it's not, rewrite URL to home.php?locale=fr and if there is locale=, it takes whatever is after that(only two characters) and put them in home.php?locale=%1 so acceuil?locale=fr means home.php?locale=fr. And acceuil?locale=en means home.php?locale=en.

There are two conditions and two rewrite rule:-D

Upvotes: 1

Related Questions