casp
casp

Reputation: 35

Htaccess rewriterule and special characters '&'

I am trying to make some really simple redirection from an old to a new website. But all my redirection with the character '&' dont work.

For exemple:

    RewriteRule ^-Nos-equipes-$ /-Associes-Collaborateurs-?lang=fr [R=301,L]

work fine, but:

    RewriteRule ^-Contact-?&lang=en$ /-Nous-contacter-?lang=en [R=301,L]

Dont work.

I am sure its easy, but i dont find anyway to make it work. I have try flag [NE] , or write [&]...

Thanks. Casp.

EDIT:

I now use the solution anubhava who works; but not totaly cause my problem is more complexe; In fact i got some url with param:

 ?&lang=en, ?&lang=fr, and ?lang=en 

So i have had other query string like that:

# Redirection from the old site #--------------------------------------
    RewriteCond %{QUERY_STRING} (^|&)lang=fr(&|$)
    RewriteCond %{QUERY_STRING} (^|&)lang=en(&|$)
    RewriteCond %{QUERY_STRING} ^lang=en(&|$)

This work still great; except in 2 cases: the first one: trying to make this redirection (stay on same url but juste modify the param)

    RewriteRule ^the_url?&lang=en$ /the_url?lang=en [R=301]

who make an redirection error.

And second case when i have to rewrite the same url but with different param:

    RewriteRule ^the_url$ /other_url?lang=fr [R=301]
    RewriteRule ^the_url?&lang=en$ /other_url?lang=en [R=301]
    RewriteRule ^the_url?&lang=fr$ /other_url?lang=fr [R=301,L]

It works but only with the first one insruction. (i dont put the flag L, cause i got a long list of rewrite; i think i can put the condition at the beginning and not rewrite them each-time)

:::::::::::::::::::::::

Exemple of real url that i am trying to redirect who cause trouble

    www.mywebsite/-Avocats-a-la-Cour- to--> www.mywebsite/?lang=fr 
    www.mywebsite/-Avocats-a-la-Cour-?&lang=en to--> www.mywebsite/?lang=en 

    www.mywebsite/Marque-francaise?&lang=fr to--> www.mywebsite//Marque-francaise?lang=fr
    www.mywebsite/French-Trademarks?&lang=en to--> www.mywebsite//French-Trademarks?lang=en

    www.mywebsite/-Contact-?&lang=en to--> www.mywebsite/-Nous-contacter-?lang=en
    www.mywebsite/-Contact-?&lang=fr to--> www.mywebsite/-Nous-contacter-?lang=fr

    www.mywebsite/person-780?lang=en to--> www.mywebsite/personel?lang=en

Upvotes: 1

Views: 414

Answers (2)

anubhava
anubhava

Reputation: 785128

You cannot match QUERY_STRING in RewriteRule. You can use:

RewriteCond %{QUERY_STRING} (^|&)lang=en(&|$)
RewriteRule ^-?Contact-?$ /-Nous-contacter- [R=302,NC,L]

QUERY_STRING is automatically carried over to target URI.


EDIT: Based on your edited question you can have these rules:

# www.mywebsite/-Avocats-a-la-Cour- to--> www.mywebsite/?lang=fr 
RewriteCond %{THE_REQUEST} \s/+(-Avocats-a-la-Cour-)\s [NC]
RewriteRule ^ /?lang=fr [R=302,L,NE]

# www.mywebsite/-Avocats-a-la-Cour-?&lang=en to--> www.mywebsite/?lang=en 
RewriteCond %{THE_REQUEST} \s/+(-Avocats-a-la-Cour-)\?&?(lang=en)\s [NC]
RewriteRule ^ /?%2 [R=302,L,NE]

# www.mywebsite/Marque-francaise?&lang=fr to--> www.mywebsite/Marque-francaise?lang=fr
# www.mywebsite/French-Trademarks?&lang=en to--> www.mywebsite/French-Trademarks?lang=en
RewriteCond %{THE_REQUEST} \s/+(Marque-francaise|French-Trademarks)\?&(lang=(?:en|fr))\s [NC]
RewriteRule ^ /%1?%2 [R=302,L,NE]

# www.mywebsite/-Contact-?&lang=en to--> www.mywebsite/-Nous-contacter-?lang=en
# www.mywebsite/-Contact-?&lang=fr to--> www.mywebsite/-Nous-contacter-?lang=fr
RewriteCond %{THE_REQUEST} \s/+(-Contact-)\?&?(lang=(?:en|fr))\s [NC]
RewriteRule ^ /-Nous-contacter-?%2 [R=302,L,NE]

# www.mywebsite/person-780?lang=en to--> www.mywebsite/personel?lang=en
RewriteCond %{THE_REQUEST} \s/+(person-780)\?&?(lang=(?:en|fr))\s [NC]
RewriteRule ^ /personel?%2 [R=302,L,NE]

Upvotes: 1

Michael Coleman
Michael Coleman

Reputation: 3398

This doesnt look right

RewriteRule ^-Contact-?&lang=en$ /-Nous-contacter-?lang=en [R=301,L]

I would think the & should only be used when you have more than one parameter being passed
e.g.

?lang=en&userId=13

I would try:

RewriteRule ^-Contact-?lang=en$ /-Nous-contacter-?lang=en [R=301,L]

Upvotes: 1

Related Questions