0

Viewing site over http and https are both working but prefer to configure it so that ALL visitors ONLY go through the SSL connection so that it always start with https://www.example.com - what am I missing?

I'm trying to do the following:
visit http ://example.com and go to https ://www.example.com
visit http ://www.example.com and go to https ://www.example.com
visit https ://example.com and go to https ://www.example.com

I created and enabled the file name examplesite (see below) in the folder /etc/apache2/sites-available

<VirtualHost example.com:80>
  ServerName www.example.com
  ServerAlias example.com
  Redirect permanent / https://www.example.com/
</VirtualHost>

<VirtualHost www.example.com:443>
  ServerName www.example.com
  DocumentRoot /var/www/example.com/public_html
  ...
  ...
</VirtualHost>
Charles
  • 41
  • 1
  • 1
  • 5
  • Thanks for your comment but I'm looking for Virtualhost configuration on apache and not mod_rewrite redirects. If you have something similar, I'll give it a try. – Charles Mar 11 '14 at 21:48
  • What is the problem you are having? – Michael Hampton Mar 11 '14 at 21:51
  • Looking to have only https viewing over www.example.com and not example.com and right now site is viewable using both http and https. – Charles Mar 11 '14 at 21:57

2 Answers2

1

looks like your first virtualhost does not meet the minimum requirements for a virtualhost

 Inside each <VirtualHost> block, you will need at minimum a ServerName directive to designate which host is served and a DocumentRoot directive to show where in the filesystem the content for that host lives.

so, add a

DocumentRoot /var/www/example.com/public_html

at the first virtualhost and see how that goes

source: apache.org

Sverre
  • 723
  • 2
  • 12
  • 23
0

Strange I am looking for the mod_rewrite method and I found this link today:

https://stackoverflow.com/questions/16200501/http-to-https-apache-redirection

VirtualHost entries require IP addresses and will not work with a fqdn. Change your

to

...

For the second part of your question, redirecting example.com to www.example.com you will need to use the rewrite module:

RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com [nocase] RewriteRule ^(.*) http://www.example.com/$1 [last,redirect=301]

That will add a www on the beginning of all addresses.

Byron C.
  • 737
  • 1
  • 7
  • 15
  • Hi Bryon - it does look similar except that I didn't have NameVirtualHost listed in the first line. Not sure if that makes a difference. Also I do have SSLEngine On since going to the https address does work. – Charles Mar 11 '14 at 23:16
  • Ah yes. My mistake. VirtualHost uses an IP address. NameVirtualHost uses the fqdn. If you change your fqdn to a * i suspect it will work. http://serverfault.com/questions/6963/what-is-the-logic-behind-virtualhost-namevirtualhost clarifies the difference. – Byron C. Mar 11 '14 at 23:20
  • Thanks to your tips @Byron, I'm getting somewhere by using *. Each scenario works except the one going from https ://example.com to https ://www.example.com – Charles Mar 11 '14 at 23:40
  • Sorry, that was a wrong comment. I think to change that you will have to use the rewrite module. That is the only way I have seen it changed. – Byron C. Mar 12 '14 at 00:15
  • Edited my answer to be more inclusive of the comments. – Byron C. Mar 12 '14 at 00:27