Support » Networking WordPress » .htaccess language redirect with multisite

  • I’m transitioning a two language (English+French) website from a older non-responsive static css/html design to something more modern and have been learning WordPress as I think it will provide some benefits.

    So far I have a multisite installation working properly using two sub-folders (/en and /fr) for their respective english and french versions. Each uses its own child template of the Twenty Twenty-Four Theme.

    In the original html website I added code to the .htaccess file to redirect visitors to the appropriate sub folder based on their browser language. Can the WordPress multisite’s .htaccess file be used to do something similar?

    I tried copying and pasting the same code used on the old static site below the multisite “WordPress” code in the .htaccess file, but it doesn’t appear to work (didn’t break anything either).

    As the “/en” and “/fr” folders are dynamically created by WP I’m thinking that an htaccess redirect based on the visitor’s browser language might not be possible.

    Is there a recommended way to do this (other than pay $$ for a plugin)?

    Thank you for any advice.

    Russell

    R

    • This topic was modified 2 weeks, 1 day ago by rpmtl22. Reason: typo
Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello,

    Redirecting users based on their browser language is indeed possible with WordPress multisite, and you’re on the right track by using the .htaccess file. However, there are a few considerations to keep in mind.

    Since WordPress multisite dynamically generates URLs for each subsite (e.g., /en/ and /fr/), trying to set up language-based redirects directly in .htaccess might be a bit tricky. Here’s an alternative approach using PHP and WordPress functions to achieve language-based redirection:

    1. Open your theme’s functions.php file for both the English and French child themes.
    2. Add the following code to detect the browser language and redirect accordingly:

    For the English version (functions.php in the /en/ theme):

    function language_redirect() {
        if ( !is_user_logged_in() && !is_admin() ) {
            $user_lang = substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 );
            if ( $user_lang == 'fr' ) {
                wp_redirect( home_url( '/fr/' ) );
                exit();
            }
        }
    }
    add_action( 'template_redirect', 'language_redirect' );
    

    For the French version (functions.php in the /fr/ theme):

    function language_redirect() {
        if ( !is_user_logged_in() && !is_admin() ) {
            $user_lang = substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 );
            if ( $user_lang == 'en' ) {
                wp_redirect( home_url( '/en/' ) );
                exit();
            }
        }
    }
    add_action( 'template_redirect', 'language_redirect' );
    

    This code checks the browser’s language preference and redirects users to the appropriate language subfolder.

    Remember to customize the code based on your specific needs and test thoroughly. Also, note that this approach assumes that the browser language setting is reliable, which may not always be the case. Users can have multiple languages set in their browser preferences, and the order of preference may vary.

    Additionally, always make a backup before modifying theme files and test thoroughly to ensure the desired behavior.
    Thank you.

    Thread Starter rpmtl22

    (@rpmtl22)

    THANK YOU Narola. You’re very generous to share your knowledge 🙂

    I’ll test this out tomorrow. I do wonder if this code will need to be added again whenever WordPress is updated? Or do the functions.php files remain the same?

    Russell

    Thread Starter rpmtl22

    (@rpmtl22)

    Assuming the PHP code works I’m left wondering what happens when visitors arrive to the root of the website?

    The code I was using in the .htaccess sent visitors to the French version and for everyone else sent them to the English version.

    RewriteEngine On
    
    RewriteCond %{HTTP:Accept-Language} ^fr [NC]
    RewriteRule ^$ /f/ [L,R=302]
    
    # else redirect to the English version
    RewriteRule ^$ /e/ [L,R=302]

    The multisite WordPress install resulted in 3 sites being created: the root of the domain (example.com) PLUS the two “sub-folder” sites within it (example.com/en and example.com/fr).

    When I currently go to example.com I land in the site root (where I’ve put an under construction page). But what will automatically redirect visitors from there to either example.com/en or example.com/fr which is where the two language versions of the website will live. I’m trying you avoid the need for a triage page that prompts visitors to click on their preferred language.

    Thread Starter rpmtl22

    (@rpmtl22)

    Would adding an “else” line to the code result in loading the /en site if the browser language check fails, or the browser language is neither ‘en’ nor ‘fr’ ?

    function language_redirect() {
        if ( !is_user_logged_in() && !is_admin() ) {
            $user_lang = substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 );
            if ( $user_lang == 'en' ) {
                wp_redirect( home_url( '/en/' ) );
            else {
                wp_redirect( home_url( '/en/' ) );
                exit();
            }
        }
    }
    add_action( 'template_redirect', 'language_redirect' );
    
    
    function language_redirect() {
        if ( !is_user_logged_in() && !is_admin() ) {
            $user_lang = substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 );
            if ( $user_lang == 'fr' ) {
                wp_redirect( home_url( '/fr/' ) );
            else {
                wp_redirect( home_url( '/en/' ) );
                exit();
            }
        }
    }
    add_action( 'template_redirect', 'language_redirect' );
    Thread Starter rpmtl22

    (@rpmtl22)

    Neither of my child themes already have function.php files so it seems I need to create them myself as per

    https://developer.wordpress.org/themes/basics/theme-functions/

    I’ll study the documentation before proceeding.

    R

    Thread Starter rpmtl22

    (@rpmtl22)

    I created the two functions.php files and placed each inside their respective child theme folder (english and french).

    Unfortunately they’re not working. If I go to example.com it loads the page I created in the site root with a language choice page.

    When I go to example.com/fr it tries to take me to example.com/fr/en/ (which does not exist) and after about 10 seconds results in “The page isn’t redirecting properly

    To confirm that the correct PHPs are being put into the correct child theme folders: visitors landing on example.com/fr are redirected to /en if their browser language is English, and French language browsers landing on example.com/en are redirected to /fr.

    Here’s what’s in the French child theme folder:

    <?php
    function language_redirect() {
        if ( !is_user_logged_in() && !is_admin() ) {
            $user_lang = substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 );
            if ( $user_lang == 'en' ) {
                wp_redirect( home_url( '/en/' ) );
                exit();
            }
        }
    }
    add_action( 'template_redirect', 'language_redirect' ););
    
    

    Thanks for any help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.