Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

In /etc/apache2/sites-available/example.com:

<VirtualHost *:443>
        ServerName www.example.com
        DocumentRoot /var/www/example.com/htdocs
        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
        SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
</VirtualHost>

I also have a virtual host configured for foo.example.com, but that only listens on port 80.

I have the A record for foo.example.com pointing to this same server. If I visit https://foo.example.com in my browser, it loads the Virtual Host for www.example.com. How can I combat this?

share|improve this question
add comment

1 Answer

This is because you have not setup the SSL for the sub domain foo.example.com and so it uses the www. domain. If you do not want SSL you can simply remove the Virtual Host all together running on port 443, otherwise just add to the configuration the following:

Allow foo.example.com to operate on SSL

<VirtualHost *:443>
        ServerName foo.example.com
        DocumentRoot /var/www/foo.example.com/htdocs
        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
        SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
</VirtualHost>

Redirect HTTPS to HTTP

<VirtualHost *:443>
        ServerName foo.example.com
        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^foo.example.com
        RewriteRule ^/(.*)$ http://foo.example.com/$1 [L,R=301]
        DocumentRoot /var/www/foo.example.com/htdocs
        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
        SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
</VirtualHost>

Or if you want a 404 Error then use:

<VirtualHost *:443>
        ServerName foo.example.com
        RewriteEngine on
        Redirect 404 /
        ErrorDocument 404 "Page Not Found"
        DocumentRoot /var/www/foo.example.com/htdocs
        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
        SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
</VirtualHost>
share|improve this answer
    
Interesting. I do not want https://foo.example.com to resolve to anything – users should get a 404. What Virtual Host should I remove in that case? –  Charlie S Mar 14 '13 at 18:55
    
Added even more solutions. –  bybe Mar 14 '13 at 19:41
    
Unfortunately, since I already have www.example.com listening on port 443, it takes precedence and I receive this error when reloading the Apache configuration: VirtualHost overlap on port 443, the first has precedence –  Charlie S Mar 14 '13 at 19:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.