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

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
  • 783
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.
  • 747