We have a site I'll call acme.com. Most of the time you see http://www.acme.com and sometimes we redirect you to https://www.acme.com.
We want to redirect anyone going to http://acme.com or http://*.acme.com to http://www.acme.com, and the same for https. (It's mainly to avoid the alert you get if you go to https://acme.com instead of https://www.acme.com)
Our vhost file is at the end of the post. It works nicely except for one strange behavior:
http://acme.com - successfully redirects to http://www.acme.com
http://www.acme.com - successfully does not redirect
http://foo.acme.com - successfully redirects to http://www.acme.com
https://acme.com - successfully redirects to https://www.acme.com
https://www.acme.com - successfully does not direct
https://foo.acme.com - ERROR - redirects to http://www.acme.com
It's this last result I can't fathom. I've tried a lot of trial and error solutions from Google & Stack Overflow but nothing seems to change it. Even if we swap the order of the configurations (so that 443 is before 80) it still redirects https://foo.acme.com to http://www.acme.com
We are running Apache/2.2.12 on Ubuntu.
Here's the configuration file:
<VirtualHost *:80>
ServerName www.acme.com
ServerAlias acme.com *.acme.com
ServerSignature On
DocumentRoot /var/www/acme.com/public
RailsEnv 'production'
PassengerHighPerformance on
<Directory /var/www/acme.com/public>
AllowOverride all
Options -MultiViews
</Directory>
SSLEngine Off
CustomLog /var/log/apache2/acme.log combined
ErrorLog /var/log/apache2/acme-error.log
# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
LogLevel warn
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^[^\./]+\.[^\./]+$
RewriteRule ^/(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</VirtualHost>
<VirtualHost *:443>
ServerName www.acme.com
ServerAlias acme.com *.acome.com
DocumentRoot /var/www/acme.com/public
RailsEnv 'production'
PassengerHighPerformance on
<Directory /var/www/acme.com/public>
AllowOverride all
Options -MultiViews
</Directory>
SSLCertificateFile /etc/ssl/certs/www.acme.com.crt
SSLCertificateKeyFile /etc/ssl/private/acme.com.private.key
SSLCACertificateFile /etc/ssl/certs/EV_intermediate.crt
SSLEngine On
CustomLog /var/log/apache2/ssl-acme.log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
ErrorLog /var/log/apache2/ssl-acme-error.log
# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
LogLevel warn
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^[^\./]+\.[^\./]+$
RewriteRule ^/(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
</VirtualHost>