.htaccesshttp-redirecthttpsno-www

HTTP to HTTPS and non-WWW to www with multiple domains


I can't find the answer anywhere for this.

I have a page with multiple domains for multi-language purpose and i have a sub-domain for each so I can develop locally (dev.)

Before the need of HTTPS it all worked fine redirecting from non-WWW to WWW and working locally with "dev.". This was (is) my .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com
RewriteRule (.*) http://www.domain1.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^domain2.com
RewriteRule (.*) http://www.domain2.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^domain3.com
RewriteRule (.*) http://www.domain3.com/$1 [R=301,L]

So what i had was: every non-subdomain woud redirect to www

Now i need the domain domain1.com to always redirect to https and also to www when there's no subdomain, so the dev. can still work.

examples:

http://domain1.com -> https://www.domain1.com/
http://www.domain1.com -> https://www.domain1.com/
http://www.domain2.com -> http://www.domain2.com/
http://domain3.com -> http://www.domain3.com/
http://dev.domain3.com -> http://dev.domain3.com/
http://dev.domain1.com -> https://dev.domain1.com/

Solution

  • this should cover it!

    RewriteCond %{HTTPS} !on
    RewriteCond %{HTTP_HOST} ^dev\.domain1\.com
    
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    
    RewriteCond %{HTTP_HOST} ^(?:[^dev]*|dev.+)(\.domain1\.com)
    RewriteRule (.*) https://www%1%{REQUEST_URI}
    
    RewriteCond %{HTTP_HOST} ^domain1.com
    RewriteRule (.*) https://www.domain1.com/$1 [R=301,L]
    RewriteCond %{HTTP_HOST} ^domain2.com
    RewriteRule (.*) http://www.domain2.com/$1 [R=301,L]
    RewriteCond %{HTTP_HOST} ^domain3.com
    RewriteRule (.*) http://www.domain3.com/$1 [R=301,L]`