Question

Ubuntu vhosts with wildcard SSL all redirecting to one domain

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

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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?