Redirect all http AND https non-www URLS to https://www.example.com via htaccess

You need a certificate that is valid for both example.com and www.example.com if you're going to rewrite those requests to www.example.com (or two separate certs that accomplish this). There's no way around this.


@Simbus82 is right about inverting the rules. Here is an approach that is slightly more universal.

RewriteEngine On
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Invert the rules ;-)

i'm using this and i had your problem

RewriteEngine On

RewriteCond %{HTTP_HOST} ^site.it [NC]
RewriteRule (.*) http://www.site.it/$1 [L,R=301]

RewriteCond %{SERVER_PORT} ^443$ [OR]
RewriteCond %{HTTPS} =on 
RewriteRule ^(.*)$ http://www.site.it/$1 [R=301,L]

First rewrite to www and then check https.