By Will

2015-09-07 08:15:25 8 Comments

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

For example should be going to

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]


@Will 2015-09-10 10:49:52

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]

@MrWhite 2018-03-05 23:06:53

Note that this will result in a double redirect when requesting (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.

@xsrf 2018-07-08 14:03:18

@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://

@MrWhite 2018-07-09 11:09:19

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

@Will 2018-07-10 11:34:37

Both good points from xsrf and MrWhite. I've updated the answer.

@Will 2018-07-12 23:23:40

@xsrf I checked the double redirect with and it appears it only redirects http:// > https:// not www.

@MrWhite 2019-04-29 23:56:13

@Will The mwl .htaccess tool does not "follow redirects", so does not make a second request in order to see the second redirect. To see the second redirect (in that tool) you need to manually create a second test with the output of the first.

@Ajay Patidar 2016-05-02 09:27:50

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: redirect to

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

Example: redirect to

RewriteEngine on

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

@MrWhite 2016-05-02 15:18:38

This doesn't handle the non-www to www redirect as stated in the question.

@Ajay Patidar 2016-07-25 08:50:56

You can add this line in htaccess RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [R=301,L]

@Ajay Patidar 2017-09-19 13:30:40

Replace RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] by RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [R=301,L]

@MrWhite 2017-09-19 13:57:08

You can edit your answer.

@MrWhite 2015-09-07 11:49:29

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 ^(.*)$$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 to 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.)

Related Questions

Sponsored Content

1 Answered Questions

2 Answered Questions

[SOLVED] Wild card redirection for HTTPS and Non-www version

  • 2014-10-06 14:43:22
  • user38723
  • 25509 View
  • 4 Score
  • 2 Answer
  • Tags:   htaccess https

2 Answered Questions

[SOLVED] Properly redirect a site to WWW and HTTPS

3 Answered Questions

[SOLVED] Force https and no www using htaccess

2 Answered Questions

1 Answered Questions

1 Answered Questions

[SOLVED] Apache rewrite rule for all pages after domain name

Sponsored Content