On my site I have installed an SSL certificate on the non www version of the domain. I would like to use htaccess to redirect http www, http non www and https www to https:// <
You can use: for http or https, with or without www -> https without www
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
And for the opposite: for http or https, with or without www -> https with www
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
As per your comments here is one single rule to do following redirects in single rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L]
This does:
http://www.domain.com/foobar => https://www.domain.com/foobar
http://domain.com/foobar => https://www.domain.com/foobar
https://domain.com/foobar => https://www.domain.com/foobar
https://www.domain.com/foobar => Unchanged