-1

My virtual host file:

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com

#Redirect non-https or non-www to https://www...
RewriteEngine On
#NOT TESTING HTTPS REDIRECTION FOR NOW
#RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,NE,R=301]
</VirtualHost>

The conf file for SSl is the default one by letsencrypt.

My tests so far, first 3 results are expected, last is not

decimoseptimo@ubuntu-512mb-sfo2-1:~$ curl -I http://example.com                                                 
HTTP/1.1 301 Moved Permanently
Location: https://www.example.com/

301

decimoseptimo@ubuntu-512mb-sfo2-1:~$ curl -I http://www.example.com
HTTP/1.1 200 OK

OK

decimoseptimo@ubuntu-512mb-sfo2-1:~$ curl -I https://www.example.com
HTTP/1.1 200 OK

OK

decimoseptimo@ubuntu-512mb-sfo2-1:~$ curl -I https://example.com
HTTP/1.1 200 OK

OK

Seems www redirection is not occuring over SSL! no idea why, apache was restarted since last configuration editing.

Any thoughts?

  • 1
    It's not working because your rewrite rules are only in ``. SSL is not running on port 80. –  Jul 21 '17 at 22:32
  • I splitted the conditions in both vhosts and it worked. Would the alternative be `` Any with issues that? or what other option would there be – Decimoseptimo Jul 21 '17 at 23:08
  • Wouldn't want to to a combined one cuz the 443 has to have the certificate config – Joe Jul 21 '17 at 23:34
  • So no alternatives other than two directives, I'm ok with that. – Decimoseptimo Jul 21 '17 at 23:39
  • Possible duplicate of [Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod\_Rewrite Rules but Were Afraid to Ask](https://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever) – Jenny D Jul 22 '17 at 14:53

1 Answers1

0

The last two tests would match the 443 VirtualHost. You have not included that in the question, but we can assume that there is not a matching RewriteCond/Rule pair in the 443 VirtualHost.

Joe
  • 1,023
  • 8
  • 11