I need to set the following rules:
redirect www to non-www (close www)
redirect http to https (close http)
Suppose I have example.com , How to set the above rules together in htaccess?
Webmasters Stack Exchange is a question and answer site for webmasters. It only takes a minute to sign up.
Sign up to join this communityI solved this problem via :
RewriteCond %{HTTP_HOST} ^(www\.example\.com)(:80)? [NC]
RewriteRule ^(.*) https://example.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} 80 [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
Here is the shorter version of your code :
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]