Looking for a way to redirect several server aliases (both www and non-www) to one non-www domain with forced SSL using Apache VirtualHosts and .htaccess. I've been searching for a while now and have found several solutions but they all work partially.
The situation is as follows, in my .conf file I have a virtual host specified as follows:
<VirtualHost *:443>
ServerName example.domain
ServerAlias *.example.domain *.exampledomain.com exampledomain.com
...
</VirtualHost>
<VirtualHost *:80>
ServerName example.domain
ServerAlias *. example.domain *.exampledomain.com exampledomain.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.domain [OR]
RewriteCond %{SERVER_NAME} =example.domain
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Next, I have the following in my .htaccess:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www.exampledomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^exampledomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.domain$
RewriteRule ^(.*)$ https://example.domain/$1 [L,R=301]
Result is as follows:
http://example.domain/ -> https://example.domain/ - (correct)
http://www.example.domain/ -> https://example.domain/ - (correct)
http://exampledomain.com/ -> http://exampledomain.com/ - Forbidden, you dont have access...
http://www.exampledomain.com/ -> http://www.exampledomain.com/ - Forbidden, you dont have access...
https://exampledomain.com/ -> https://example.domain/ - (correct)
https://www.exampledomain.com/ -> https://www.exampledomain.com/ - Connection not secure
I really can't figure out where this goes wrong and why some redirects work and others don't. Any hints would be much appreciated.