I have been trying to search but no luck yet. Found different options about redirecting but nothing like what I’m looking for.
So currently the website URLs contain lang
parameter like ?lang=en
, ?lang=ru
, ?lang=fi
. Parameters are on the very end of the URL.
Idea is to move languages to top level domain. So basically I’m looking for a way to redirect all URLs that contain parameter ?lang=ru
to top-level .ru
domain. Same with other languages.
Can I do it via .htaccess
or it shouldn’t be done at all? Moving site to different domains should need redirection to pass the link juice and authority to new domains.
Hopefully, someone can lead me to the correct way of doing it.
Much appreciated!
2
Answers
You should be able to do this via .hataccess. You can use the code below in your .htaccess
I think it should work fine if you write same for all the others. DO give it a try and let me know.
If there are no other URL parameters that need to be preserved then you can do it like the following using mod_rewrite near the top of your
.htaccess
file:The above matches any language code that consists of 2 lowercase letters. To match specific language codes then use alternation in the regex and change the
RewriteCond
directive to read:The
QSD
flag discards the originallang=xx
query string from the redirect response (Apache 2.4). Otherwise this will be copied onto the target URL by default.The
%1
backreference contains the value of thelang
URL parameter captured in the preceding condition. TheREQUEST_URI
server variable contains the full URL-path (no query string) from the request.This does assume that the language specific TLD domains are hosted elsewhere. In other words, we do not need to check whether we are already at the required TLD domain.
Test with a 302 (temporary) redirect and only change to a 301 (permanent) redirect – if that is the intention – once you have confirmed that this works OK.
UPDATE: Any specific redirects, eg.
lang=en
to.com
will need to appear first. For example:Use alternation (as mentioned above) if there are more language codes. eg.
(en|gb)
.