I'm trying to do a redirect 301 from this links in my .htaccess file.
Old url: https://www.example.com/?lang=en
New url: https://www.example2.com/en/
I'll try to use this instruction:
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^lang=en$
RewriteRule ^(.*)$ /en/ [R=301,L]
like answer of this question (301 redirect from URL with query string to new domain with different query string), but my result is this: https://www.example2.com/en/?lang=en.
How can i remove the querystring "?lang=en" and obtain the new url?
Now it works with add the flag QSD, but now i've this problem:
I need to redirect this links too :
Old url 2: https://www.example.com/?lang=de
New url 2: https://www.example2.com/de/
And this links:
Old url 3: https://www.example.com/?lang=ru
New url 3: https://www.example2.com/ru/
If i use the instruction above, changing the query string, the redirect doesn't work for all of the language.
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^lang=en$
RewriteRule ^(.*)$ /en/ [R=301,L,QSD]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^lang=de$
RewriteRule ^(.*)$ /de/ [R=301,L,QSD]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^lang=ru$
RewriteRule ^(.*)$ /ru/ [R=301,L,QSD]
How can i resolve this new problem?
EDIT 2:
Solution for @QuickBecko
There are two solutions, depending on the Apache version:
First solution
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^lang=en$
RewriteRule ^(.*)$ /en/ [R=301,L,QSD]
RewriteCond %{QUERY_STRING} ^lang=de$
RewriteRule ^(.*)$ /de/ [R=301,L,QSD]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^lang=ru$
RewriteRule ^(.*)$ /ru/ [R=301,L,QSD]
Second solution
RewriteCond %{QUERY_STRING} ^lang=en$
RewriteRule ^(.*)$ /en/? [R=301,L]
RewriteCond %{QUERY_STRING} ^lang=de$
RewriteRule ^(.*)$ /de/? [R=301,L]
RewriteCond %{QUERY_STRING} ^lang=ru$
RewriteRule ^(.*)$ /ru/? [R=301,L]