8

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

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 '18 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 '18 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 '18 at 11:09
  • Both good points from xsrf and MrWhite. I've updated the answer. – Will Jul 10 '18 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 '18 at 23:23
5

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.)

1

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 '16 at 15:18
  • You can add this line in htaccess RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [R=301,L] – Ajay Patidar Jul 25 '16 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] – Ajay Patidar Sep 19 '17 at 13:30
  • You can edit your answer. – MrWhite Sep 19 '17 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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