Configuring Multiple Virtual Hosts in Apache


(Tbaatar) #1

Matrix Version:5.5.5.1

Hi all,

I know this question is not specific to Matrix, but I followed all the tutorials on the interwebs and I can’t get multiple virtual hosts with with port 80 (HTTP) to redirect to the HTTPS version of the site.

In theory this is what should happen when a user visits the website:

http://domain1.com > https://domain1.com
http://domain2.com > https://domain2.com
http://domain3.com > https://domain3.com

But when accessing the domains without the HTTP(S) protocol, the user is always redirected to the domain1.com

http://domain1.com > https://domain1.com
http://domain2.com > https://domain1.com
http://domain3.com > https://domain1.com

This is sample Virtual Host blocks for all 3 domains.

<VirtualHost *:80>
ServerName domain1.com
ServerAlias www.domain1.com

Redirect permanent / https://www.domain1.com/

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{domain1.com} [L,NE,R=301]
...
</VirtualHost>


<VirtualHost *:80>
ServerName domain2.com
ServerAlias www.domain2.com

Redirect permanent / https://www.domain2.com/

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{domain2.com} [L,NE,R=301]
...
</VirtualHost>

<VirtualHost *:80>
ServerName domain3.com
ServerAlias www.domain3.com

Redirect permanent / https://www.domain3.com/

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{domain3.com} [L,NE,R=301]
...
</VirtualHost>

Am I missing something?

Thank you.