Ssl – Apache redirect multiple domain names from https

httpsredirectssl

My server distribute two main websites, says : www.google.com & www.facebook.com (yeah I know :p)

I want them to be distributed via https. Using Apache, I defined a vhost file in sites-available/enabled containing this :

<VirtualHost *:80>
        ServerName google.com
        Redirect / https://www.google.com/
</VirtualHost>

<VirtualHost *:80>
        ServerName facebook.com
        Redirect / https://www.facebook.com/
</VirtualHost>

<VirtualHost *:80>
        DocumentRoot /srv/www/google/www/
        ServerName www.google.com
        ServerAlias www.facebook.com

        <Directory ... />
        # Google & Facebook points to the same directory (crazy right ?)

        # Next of the config
</VirtualHost>

<VirtualHost *:443>
        SSLEngine On
        SSLCertificateFile /path/to/google.crt
        SSLCertificateKeyFile /path/to/google.key

        DocumentRoot "/srv/www/google/www/"
        ServerName www.google.com

        <Directory .../>
        # Next of the config
</VirtualHost>

<VirtualHost *:443>
        SSLEngine On
        SSLCertificateFile /path/to/facebook.crt
        SSLCertificateKeyFile /path/to/facebook.key

        DocumentRoot "/srv/www/google/www/"
        ServerName www.facebook.com

        <Directory .../>
        # Next of the config
</VirtualHost>

BUT :

  • If I access to https://facebook.com, it fails saying that the SSL connection is not what expected : Google.com instead of facebook.com

Based on my configuration file, I understand why, so I tried to add :

<VirtualHost *:443>
        SSLEngine On
        ServerName facebook.com
        Redirect / https://www.facebook.com/
</VirtualHost>

But then, I can't even access facebook.com nor www.facebook.com via http/https.

So my question is quite simple : how can I redirect all https access to facebook.com (and eventually all sub facebooks : facebook.fr, www.facebook.fr, etc) to www.facebook.com (redirecting to www domain) in HTTPS ?

Thanks for your help ! 🙂

Best Answer

That's because you didn't configure a virtualhost for facebook.com, so apache picks the first configured one (www.google.com). What you're looking for is the ServerAlias configuration directive.