frankie frankie - 2 months ago 11
Apache Configuration Question

.htaccess redirect from index.php not working

I did my best to search in stack overflow questions and answers and even when I tried to apply a solution, it is not working.

I need to redirect all adresses, that begin with

http://www.example.com/index.php?lang=XX

to

http://www.example.com/XX

This is what I try:

RewriteCond %{HTTP_HOST} ^www.example\.com [NC]
RewriteRule ^(index\.php\?lang\=)$ http://www.example.com/$1 [L,R=302]


The location /index.php?lang=en is not redirecting /en (anywhere).
It is at the same URL.

Answer

You can't match a query_string in a rewriterule. You need a specific RewriteCond for it. It should be more or less like this:

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{QUERY_STRING} ^lang=(.*)
RewriteRule ^ http://www.example.com/%1 [R=302,L,QSD]