Ssl – Apache won’t redirect HTTP to HTTPS

apache-2.2apache-2.4Apache2httpsssl

When I enter my website's non-SSL URL "cms00.example.com" into my browser, it won't redirect to https://cms00.example.com. If I enter the HTTP address, I can see the site and if I enter the HTTPS address, I can see the site. I just can't get the redirect from http to https to work. I've read numerous articles on how to do this and tried all the suggestions but my configuration still isn't working. I'm running Apache 2.4.10 on Debian 8, and this is my first time working with Apache.

I've run these two commands and verifed that the rewrite and ssl modules have been loaded:

sudo a2emod rewrite   # <- already enabled
sudo a2emod ssl       # <- already enabled

I haven't made any modifications to /etc/apache2/apache2.conf and I haven't created any additional .htaccess files.

Here is my configuration file:

# /etc/apache2/sites-available/vhosts.conf
DirectoryIndex index.php index.html

<VirtualHost *:80>
  ServerName cms00.example.com
  DocumentRoot "/var/www/html"
  Redirect permanent / https://cms00.example.com
</VirtualHost>

<VirtualHost *:443>
  ServerName cms00.example.com
  DocumentRoot "/var/www/html"

  SSLEngine on
  SSLCipherSuite AES256+EECDH:AES256+EDH
  SSLProtocol All -SSLv2 -SSLv3
  SSLHonorCipherOrder On
  SSLCompression off
  SSLCertificateFile /etc/apache2/ssl/example.com.crt
  SSLCertificateKeyFile /etc/apache2/ssl/private/example.com.key

  <Directory "/var/www/html">
    AllowOverride All
    Options -Indexes +FollowSymLinks
    Require all granted
  </Directory>
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

Best Answer

I've solved the problem. Here's what's going on. /etc/apache2/apache2.conf includes a call to any config files that have symlinks in /etc/apache2/sites-enabled. Since there was a symlink in that directory pointing to /etc/apache2/sites-available/000-default.conf, that latter config file was being loaded and it was over-riding the blocks and directives in my vhosts.conf file. Once I deleted that symlink, my vhosts.conf settings were able to take effect. The lesson for me was that any file that has a symlink in sites-enabled will be enabled.