301 redirect to https for WordPress multisite

The multisite mode in this case is when the main domain and its subdomains work in the same base and the subdomains are a mirror of the main site. If I add a standard 301 redirect to .htaccess then subdomains via http subdomains start redirecting to the main site.

This is correct because the redirect looks like this one:

RewriteCond %{SERVER_PORT} ^80$ [OR]
RewriteCond %{HTTP} =on
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]

In the case of multisite, delete the example above and then add the following code to functions.php:

function force_https () {
 if ( !is_ssl() ) {
  wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
  exit();
 }
}
add_action ( 'template_redirect', 'force_https', 1 );

Now the main site and all multisite ‘clone’ subdomains have their own redirects.

About iryna

I'm Iryna, a web developer from Ukraine with a decade of experience solving complex technical challenges in the world of freelance. Throughout my career, I've worked on everything from troubleshooting server-side issues and optimizing website performance to enhancing user interfaces. On this blog, I share detailed solutions to the technical problems I’ve encountered and methods that have worked best for me. In addition to my technical expertise, I’m also passionate about digital drawing. I hope the tutorials and insights I provide here will help both fellow developers and creatives alike in their own projects.

Check Also

How to remove 404 //fonts.gstatic.com and //fonts.googleapis.com from WordPress head section

I tried solutions with functions that should remove from head and , but a very …

Leave a Reply

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