Ubuntu vhosts with wildcard SSL all redirecting to one domain

July 18, 2016 376 views
Apache Ubuntu

hi there,
Am having an issue that i've been banging my head on the past weekend...
I've setup 2 vhosts on a newly configured ubuntu 14 server, one is mean to be a primary domain www.mydomain.com, and the other is meant to be a subdomain, sub.mydomain.com. There is a wildcard SSL certificate that both vhosts shares.

Problem: When i navigate to (i've masked my host file to point the domains to my server IP for this testing) sub.mydomain.com, the browser is always redirected to www.mydomain.com.

Expected behavior: When i navigate to sub.mydomain.com, it should not redirect to the primary domain, but rather load the site that is hosted at the sub domain.

The vhost configurations for both domains are as follows:
www.mydomain.com:

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

    #Redirect everything to https:
    RewriteEngine on
    RewriteRule ^(.*)$ https://www.mydomain.com$1 [R=301,L]

</VirtualHost>
<VirtualHost mydomain.com:443>
    ServerName mydomain.com
    ServerAlias www.mydomain.com

    ServerAdmin admin@mydomain.com
    DocumentRoot /var/www/mydomain.com/html

    <Directory /var/www/mydomain.com/html>
            Options Indexes FollowSymLinks MultiViews
                AllowOverride All
        Require all granted
    </Directory>

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !www.mydomain.com
    RewriteRule ^(.*)$ https://www.mydomain.com$1 [R=301,L]

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/mydomain.com.crt
    SSLCertificateKeyFile /etc/apache2/ssl/mydomain.com.key
    SSLCACertificateFile /etc/apache2/ssl/intermediate.crt
</VirtualHost>

sub.mydomain.com:

<VirtualHost *:80>
   ServerName sub.mydomain.com

    #Redirect everything to https:
    RewriteEngine on
    RewriteRule ^(.*)$ https://sub.mydomain.com$1 [R=301,L]
</VirtualHost>
<VirtualHost sub.mydomain.com:443>
    ServerName sub.mydomain.com

    ServerAdmin admin@mydomain.com
    DocumentRoot /var/www/sub.mydomain.com/html

    <Directory /var/www/sub.mydomain.com/html>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Require all granted
    </Directory>


    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/mydomain.com.crt
    SSLCertificateKeyFile /etc/apache2/ssl/mydomain.com.key
    SSLCACertificateFile /etc/apache2/ssl/intermediate.crt
</VirtualHost>

Can anyone let me know what im doing wrong and offer some assistance? The SSL cert works as we are currently using it on another server on Mediatemple, with the same domains. We're looking to migrate those 2 sites to DO so we want the same SSL and security setup.

thanks

1 comment
  • To see what's happening to your request from the client side, use verbose mode with curl:

    curl --verbose http://sub.mydomain.com
    

    From there, you may be able to look at the apache logs to correlate what you're seeing from the client side.

1 Answer

hm...unfortunately, when i do a curl, it just hits my current live website. So i cant tell what the issue is. Im assuming you mean to run that command from the server itself?

Have another answer? Share your knowledge.