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?


2 Answers 2


I 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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.