Forum Moderators: phranque
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] [edited by: not2easy at 3:09 pm (utc) on May 11, 2020]
[edit reason] exemplified and formatted for readability [/edit]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
The condition should be expressed as a negative, meaning “anything other than this one specific form”: depending on site, either RewriteCond %{HTTP_HOST} !^example\.com$
or RewriteCond %{HTTP_HOST} !^(example\.com)?$
On a shared server using a VirtualHost, you don’t need the (blahblah)? element, but it does no harm and makes some people feel safer. You can also argue about the [NC]; human browsers tend to flatten casing of hostnames, so the only requests for EXAMPLE.COM are from robots that will be blocked anyway.