How to properly force https and www on multisite with Apache HTAccess

I have a wordpress multisite in network mode via wp multi network plugin.

I’ve been trying to use .htaccess rules to force all urls to be https://www.

eg:

  • https://domain.com to https://www.domain.com
  • http://domain.com to https://www.domain.com
  • http://www.domain.com to https://www.domain.com

I’ve found these variations online but I’m not sure what would be the cleanest way to do this?

## force HTTPS and www
RewriteCond %{HTTP_HOST} (?!^www\.)^(.+)$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^(dev|www)\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI}/ [R=302,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}/ [R=302,L]
RewriteCond %{HTTP_HOST} !^(dev|www)\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI}/ [R=302,L]

What’s the best way to accomplish this?
Thank you

1 Answer
1

First one of your variations is the cleanest.
You can test your htaccess rewrite rules here:
https://htaccess.madewithlove.be/

Important thing about R=301, this means the redirection is permanent.

Leave a Comment