Htaccess rewriterule and special characters '&' - .htaccess

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

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]

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]

Related

What's wrong with this RewriteRule?

Here's my code in htaccess:
RewriteCond %{THE_REQUEST} \s/user/\?user=(.*)\s [NC]
RewriteRule ^ /user/%1? [R=301,L]
RewriteRule ^user/(.*)$ /user/?user=$1 [L]
What I want it to do is take the URL /user/?user=username and rewrite it to /user/username
What I have successfully rewrites it to /user/username but then it gives me a 500 error. If anyone could tell me why I would be very appreciative.
Thanks!
Edit:
The 500 error seems to be because it's creating a redirect loop with this rule. I'm not sure how to take just the last part and append it to the URL as a query string.
Please change this line :
RewriteRule ^user/(.*)$ /user/?user=$1 [L]
with this :
RewriteRule ^user/[a-zA-Z]+$ /user/?user=$1 [L]
You should match only letters , otherwise the above condition RewriteCond %{THE_REQUEST} \s/user/\?user=(.*)\s [NC] doesn't make any sense with (.*)$

Redirecting %{QUERY_STRING} to friendly url

I'm trying to add one last improvement (regarding htaccess), and that is that I would like it to redirect /?mod=inicio to /inicio. So I'm trying to do it with the following code, but It keeps building the url like this /inicio?mod=inicio
RewriteCond %{QUERY_STRING} ^mod=([a-z]+)$ [NC]
RewriteRule .* /%1 [L]
Same thing with extra parameters: from /?mod=x&type=y-z to /x/y-z
Try these rules instead:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?mod=([a-z]+)&type=([a-z\-]+)
RewriteRule ^ /%1/%2? [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?mod=([a-z]+)($|\ )
RewriteRule ^ /%1? [L]
The key is to match against the actual request instead of the URI because your other rules (from your previous question) will cause these rewrites to match, they'll conflict with each other. The other key point is to include a ? at the end of the targets (e.g. /%1/%2?) which makes it so the query string doesn't get appended.

HtAccess rewriting, removing a part of the text between the first two slashes and adding index.php after it

I got the following url:
127.0.0.1/abc_123456/default/index/index/
Which should be rewritten to:
127.0.0.1/123456/index.php/default/index/index/
So remove abc_ and add index.php after it. Problem is that the digits are variable, but the abc_ isn't.
I had the following rule:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /abc_
RewriteRule ^abc_(.*)/(.*)$ /$1/index.php/$2
But that resulted in the url being rewritten to:
127.0.0.1/123456/default/index/index.php/index/
Seems like I'm almost there, but I can't figure it out.
Thanks in advance
Use this simple rule:
RewriteRule ^abc_([0-9]+)/(.*)$ $1/index.php/$2 [L,NC]
Try
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /abc_([0-9]+)/([^\ \?]+) [NC]
RewriteRule ^ /%1/index.php/%2 [L]
EDIT
However, you are right about the other rule, that's the one giving the error; RewriteCond %{THE_REQUEST} !^[A-Z]+\ /abc_ RewriteRule ^(.*)$ /index.php/$1 That one is used to rewrite if the page does not contain the /abc_123456/
Add an extra condition to that rule as below
#if not abc_
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /abc_ [NC]
#if not already index.php
RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
for example, if you need similar solution, when visited this url:
http://yoursite.com/subpage1/subpage2/?YOURSTRING=blabla
to redirected visitor to
http://yoursite.com/subpage1/subpage2/
then see link - http://stackoverflow.com/a/15680832/2215124

Simple Htaccess question

RewriteEngine on
# if the following conditions are met, SKIP the rewriteRules.
RewriteCond %{THE_REQUEST} \?abc=xyz(&(app=([a-z]+))) #i don't know it is right or wrong
########LOGIN########
RewriteCond %{THE_REQUEST} \?event_id=156&rp=([a-zA-Z0-9=]+)
RewriteRule ^events/login.php$ http://www.xyz.com/is2011/login.php?rp=%1 [R=301,L]
########SEARCH########SEARCH########################
RewriteCond %{THE_REQUEST} \?search=([a-z]+)&event_id=156&submit=Search
RewriteRule ^is2011/([a-z]+).php$ http://www.xyz.com/is2011/$1.php?search=%1 [R=301,L]
I just want to skip above rewrite rules if and only if there is app=xyz in query string.
Would be:
RewriteEngine on
# if the following conditions are met, SKIP the rewriteRules.
RewriteCond %{THE_REQUEST} \?abc=xyz(&(app=([a-z]+))) #i don't know it is right or wrong
RewriteRule ^(.*)$ - [skip=2]
########LOGIN########
RewriteCond %{THE_REQUEST} \?event_id=156&rp=([a-zA-Z0-9=]+)
RewriteRule ^events/login.php$ http://www.xyz.com/is2011/login.php?rp=%1 [R=301,L]
########SEARCH########SEARCH########################
RewriteCond %{THE_REQUEST} \?search=([a-z]+)&event_id=156&submit=Search
RewriteRule ^is2011/([a-z]+).php$ http://www.xyz.com/is2011/$1.php?search=%1 [R=301,L]
The rewrite rule i added does not make any substitution in the query string matched by its RewriteCond. The skip flag skips the next num rules, in this case two. See RewriteRule's directive documentation at http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule.

RewriteRule - a.php?a=123 to /b/123

I am trying to get Apache to redirect /a.php?a=123 to /b/123 (where 123 could be any number between 1 and 9999) but can't seem to get it to work.
This is what I have in htaccess:
RewriteEngine on
RewriteRule ^a.php?a=([0-9]+) /b/$1 [L]
RewriteRule ^a.php$ /c/ [L]
With this going to a.php?a=123 results in 404, but going to just a.php works as expected.
I tried escaping the ? (RewriteRule ^a.php\?a=([0-9]+) /b/$1 [L]) but it still doesn't work.
What am I doing wrong please?
The query string is not part of the URI path that is tested in the RewriteRule directive. This can only be tested with a RewriteCond directive:
RewriteCond %{QUERY_STRING} ^a=([0-9]+)$
RewriteRule ^a\.php$ /b/%1? [L,R]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^a\.php$ /c/ [L,R]
But if you want it the other way (requests of /b/123 are redirected to /a.php?a=123):
RewriteRule ^b/([0-9]+)$ a.php?a=$1 [L]

Resources