Search code examples
.htaccessmultiple-domains

htaccess multiple domains redirect to https://www and to specific document root


I have 3 LIVE domains that point to the same document root where CMS handles each domain with it's own content.

I now need to redirect all 3 domains to HTTPS with WWW and have the the option to change each domains document root in the future.

Example what needs to happen:

http://domain1.com => https://www.domain1.com

http://www.domain1.com => https://www.domain1.com

Same must also happen for domain2.com and domain3.com

Document root for all three domain is /cms-1.1/

In the future there might be a change for domain1.com to a new document root /cms-2.1/ but for the rest of domains the document root will stay the same /cms-1.1/

Can I write one rule or multiple rules... for each domain separately or combined??

So far I came up to here:

  • for document root: (for each domain)

    RewriteCond %{HTTP_HOST} ^domain1.com$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^www.domain1.com$
    RewriteCond %{REQUEST_URI} !^/cms-1.1/*$
    RewriteRule ^(.*)$ /cms-1.1/$1 [L]
    
  • for non-www to www: (for each domain)

    RewriteCond %{HTTP_HOST} ^domain1\.com$ [NC]
    RewriteRule (.*) http://www.domain1.com/$1 [R=301,L]
    
  • for HTTPS: (for each domain)

    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://www.domain1.com/$1 [L,R=301] 
    

But will this work ??? Since all three domains are live, I need not to have any downtime...


Solution

  • you can use this generic code (not tested)

    # https mechanism
    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    #none www to www.
    RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
    RewriteRule ^(.*) https://www.%1%{REQUEST_URI} [R=301,NE,L]
    
    #Document root each domain
    RewriteCond %{HTTP_HOST} ^domain1.com$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^www.domain1.com$
    RewriteCond %{REQUEST_URI} !^/cms-1.1/*$
    RewriteRule ^(.*)$ /cms-1.1/$1 [L]