skip to Main Content

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


  1. You should be able to do this via .hataccess. You can use the code below in your .htaccess

    RewriteEngine on
    
    RewriteCond %{REQUEST_URI} lang=en
    RewriteRule ^ https//wwww.yourwebsite.ext [L,R]
    

    I think it should work fine if you write same for all the others. DO give it a try and let me know.

    Login or Signup to reply.
  2. 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:

    RewriteEngine On
    
    # Redirect "/foo?lang=xx" to "example.xx/foo"
    RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
    RewriteRule ^ https://example.%1%{REQUEST_URI} [QSD,R=302,L]
    

    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:

    RewriteCond %{QUERY_STRING} ^lang=(en|ru|fi|abc|etc)$
    

    The QSD flag discards the original lang=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 the lang URL parameter captured in the preceding condition. The REQUEST_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:

    # Redirect languages to .com
    RewriteCond %{QUERY_STRING} ^lang=(en)$
    RewriteRule ^ https://example.com%{REQUEST_URI} [QSD,R=302,L]
    
    # All other language codes...
    # Redirect "/foo?lang=xx" to "example.xx/foo"
    RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
    RewriteRule ^ https://example.%1%{REQUEST_URI} [QSD,R=302,L]
    

    Use alternation (as mentioned above) if there are more language codes. eg. (en|gb).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search