EDITED (2016.02.25): in order to prevent “HTTP forwarding PLAINTEXT” warnings, we updated and reordered the code. Both examples work, we didn’t delete the original so you can compare. See below…
Some htaccess code to add/remove “www” from domain, and to turn on/off HTTPS/SSL.
Make sure to turn on rewrite engine if it hasn’t been
RewriteEngine On
RewriteBase /
REMOVE www from domain
## REMOVE www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
ADD www to domain
## ADD www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Turn ON HTTPS/SSL
## Turn ON HTTPS/SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Turn OFF HTTPS/SSL
## Turn OFF HTTPS/SSL
RewriteCond %{HTTPS} =on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Place what you need into htaccess file just above line that says # BEGIN WordPress.
Example (comment/delete unnecessary parts):
## BEGIN Oz
RewriteEngine On
RewriteBase /
## REMOVE www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
## ADD www
# RewriteCond %{HTTP_HOST} !^www\.
# RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
## Turn ON HTTPS/SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
## Turn OFF HTTPS/SSL
# RewriteCond %{HTTPS} =on
# RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
## END Oz
Updated code:
## BEGIN Oz
RewriteEngine On
RewriteBase /
## Turn ON HTTPS/SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
## Turn OFF HTTPS/SSL
# RewriteCond %{HTTPS} =on
# RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
## Set protocol variable - always uncommented
RewriteCond %{HTTPS}s ^(on(s)|offs)$
RewriteRule ^ - [env=proto:http%2]
## REMOVE www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ %{ENV:proto}://%1/$1 [R=301,L]
## ADD www
# RewriteCond %{HTTP_HOST} !^www\.
# RewriteRule ^ %{ENV:proto}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
## END Oz