Installed SSL on Virtualhost. But Unless HTTPS Protocal Specified, User Redirected to Wrong Site

I think I fit the gist of the problem into the post title. I'm running Apache off of Ubuntu 14.01

I installed SSL on a Virtualhost previously running on port 80. The port is now running on port 443 as it should. When I enter the URL along with the HTTPS protocal into the address bar, everything works fine. But if I omit HTTPS, it redirects to another one of my virtualhosts.

I know it's a problem with the using port 443 and that Apache is rather choosing a virtualhost with port 80. Is my only option to alter the .htaccess file, or there a better way to address the issue?

Here's a pseudo mockup of my current setup under sites-enabled

 <virtualhost *:443="">SSLEngine On
  SSLCertificateFile /path/to/cert
  SSLCertificateKeyFile /path/to/keyfile
  SSLCACertificateFile /path/to/CAcert

  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin mysite@test.com
  ServerName  www.mysite@test.com
  ServerAlias  mysite@test.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex na.html na.php
  DocumentRoot /path/to/mysite@test.com/root</virtualhost> 

4 Replies

Virtualhosts are port-specific, if you have no virtualhost for that domain configured under port :80 it will send all traffic over http:// to your default web docroot.

 <virtualhost *:80="">ServerName www.mysite@test.com
    ServerAlias  mysite@test.com
    Redirect permanent / https://www.mysite@test.com/</virtualhost> 

Thanks for the reply synapt. From what I understand, you're saying I should have two virtualhosts configured for this domain, one for port 443 and the other 80 which provides the redirect using https protocal.

I'm assuming I place your example code in the same .conf file as my port 443 virtualhost. Is the below code correct?

 <virtualhost *:80="">ServerName www.mysite@test.com
   ServerAlias  mysite@test.com
   Redirect permanent / https://www.mysite@test.com/</virtualhost> 

 <virtualhost *:443="">SSLEngine On
  SSLCertificateFile /path/to/cert
  SSLCertificateKeyFile /path/to/keyfile
  SSLCACertificateFile /path/to/CAcert

  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin mysite@test.com
  ServerName  www.mysite@test.com
  ServerAlias  mysite@test.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex na.html na.php
  DocumentRoot /path/to/mysite@test.com/root</virtualhost> 

Indeed, looks like that should work fine.

Great I'll give it a shot this evening. Thank you for your help!

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct