Get a free advice now!

    Pick the topic

    Developer OutsourcingWeb developingApp developingDigital MarketingeCommerce systemseEntertainment systems

    Thank you for your message. It has been sent.

    Tags

    Multisite – redirect from the main site

    Multisite – redirect from the main site

    CHALLENGE: WordPress multisite root url should redirect to a subsite

    SOLUTION: add a wp action and wp_safe_redirect()

    Multisite WordPress setup allows to create a multilanguage website. You can set up CMS to have a language version path as a subdirectory, ex: mysite.com/de/ , mysite.com/en/ . But what will happen when trying to access a root site url mysite.com? The main blog multisite homepage will be displayed. We can use the main site as a default landing page / language switcher, or even use it as a separate language version. Another solution will be to redirect every request from the main blog → to the homepage of the sub-site.

    Redirect from the main blog

    Our multisite uses sub-directories configuration. Every language sub-blog uses 2 letter code as a root path. We decided to redirect traffic from the main blog to the EN version (blog_id = 2). That way, if somebody types a url of a raw domain, he will be redirected to the sub-blog.

    // functions.php
    // redirection for: mysite.com → mysite.com/en/
    function redirect_from_main_blog()
    {
        $blog_id = get_current_blog_id();
        if (is_home() || is_404()):
            if ($blog_id == 1 && !is_admin() && !defined('WP_CLI')) {
                $url = get_home_url(2); //redirect to EN site from base url
                echo $url;
                wp_safe_redirect($url, 301);
                exit;
            }
        endif;
    }
    add_action('wp', 'redirect_from_main_blog');

    Request Params conflicts

    Sometimes the main site root url is used by external integrations, like the Pardot Form. On form submission, the site redirects to the main url with response params added to the request. If wp_safe_redirect() is executed, we will lose data from the response. The solution will be to check if param is set and add a code exception.

    function redirect_from_main_blog() {
        $blog_id = get_current_blog_id();
        if(isset($_POST['ct_success_url'])){
            // pardot success redirection fix (HTTP_REFERER)
            return false;
        }
        if(isset($_GET['errorMessage'])){
            // pardot errorMsg redirection fix
            return false;
        }
      (...)
    }

    That’s it for today’s tutorial. Make sure to follow us for more useful tips and guidelines.

    Comments
    0 response

    Add comment

    Your email address will not be published. Required fields are marked *

    Popular news

    A game changer for WooCommerce users
    • Our Highlights

    A game changer for WooCommerce users

    March 24, 2023 by createIT
    The differences between a web app and a mobile app
    • Services
    • Technology
    • Trends

    The differences between a web app and a mobile app

    March 7, 2023 by createIT
    Webrooming and showrooming
    • Trends

    Webrooming and showrooming

    February 14, 2023 by createIT
    PHPStorm – fix long load time of a directory window
    • Dev Tips and Tricks

    PHPStorm – fix long load time of a directory window

    January 20, 2023 by createIT
    reCAPTCHA v3 – WordPress implementation
    • Dev Tips and Tricks

    reCAPTCHA v3 – WordPress implementation

    January 20, 2023 by createIT
    How to compare GIT and server files
    • Dev Tips and Tricks

    How to compare GIT and server files

    January 19, 2023 by createIT
    How to trigger a click event inside iframe?
    • Dev Tips and Tricks

    How to trigger a click event inside iframe?

    January 19, 2023 by createIT
    FOOEvents – generate a custom CSV report
    • Dev Tips and Tricks

    FOOEvents – generate a custom CSV report

    January 19, 2023 by createIT
    Headless chrome – testing webgl using playwright
    • Dev Tips and Tricks

    Headless chrome – testing webgl using playwright

    January 18, 2023 by createIT
    Preview big SQL files with PilotEdit
    • Dev Tips and Tricks

    Preview big SQL files with PilotEdit

    January 18, 2023 by createIT

    Support – Tips and Tricks
    All tips in one place, and the database keeps growing. Stay up to date and optimize your work!

    Contact us