I want a rewrite rule that redirects everything to https:// AND www.

For example http://example.com should be going to https://www.example.com

This is what I have:

RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ "https\:\/\/www\.example\.com\/$1" [R=301,L]

3 Answers 3


I found the solution.

Without HSTS (single redirect):

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

With HSTS (double redirect):

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  • 1
    Note that this will result in a double redirect when requesting http://example.com (ie. HTTP and non-www), since it will first redirect to HTTPS on the same host (ie. without www) and then redirect to www as a second redirect. You can correct this by simply reversing these two rules.
    – MrWhite
    Mar 5, 2018 at 23:06
  • @MrWhite @will you actually want a double redirect on a proper https:// redirect, especially if you use HSTS. This way, your HSTS preference can be stored by the browser for both your domains and subsequent requests will always default to https://
    – xsrf
    Jul 8, 2018 at 14:03
  • @xsrf It's still "proper" (and more efficient) to have a single www and HTTPS redirect. It only needs to be kept separate when you are implementing HSTS.
    – MrWhite
    Jul 9, 2018 at 11:09
  • Both good points from xsrf and MrWhite. I've updated the answer.
    – Will
    Jul 10, 2018 at 11:34
  • @xsrf I checked the double redirect with htaccess.madewithlove.be and it appears it only redirects http:// > https:// not www.
    – Will
    Jul 12, 2018 at 23:23

Your conditions are implicitly AND'd and your second condition will always be true (unless you have other domains), so your current rules will only redirect non-SSL traffic.

You need to OR the conditions and negate the www (second) condition:

RewriteEngine On
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

If the SERVER_PORT is not 443 (ie. is not HTTPS) or the host does not start with www. (ie. you are accessing the bare domain) then redirect to the canonical URL.

However, whether this will redirect https://example.com to https://www.example.com will depend on your security certificate. (Your site needs to be accessible by both www and non-www over SSL for the .htaccess redirect to trigger.)


This will use for both www or non-www If you try to open link with www then url redirect to https with www

Example: http://domain.com redirect to https://domain.com

or If you try to open link with non-www then url redirect to https with non-www

Example: http://www.domain.com redirect to https://www.domain.com

RewriteEngine on

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  • This doesn't handle the non-www to www redirect as stated in the question.
    – MrWhite
    May 2, 2016 at 15:18
  • You can add this line in htaccess RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [R=301,L] Jul 25, 2016 at 8:50
  • Replace RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] by RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [R=301,L] Sep 19, 2017 at 13:30
  • You can edit your answer.
    – MrWhite
    Sep 19, 2017 at 13:57

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