1

I have similar issue on one of my domains. I have SSL cert installed both for naked and www-domain.

Now, however I want to redirect both naked http://domain.com and https://domain.com to https://www.domain.com.

I have this rules in my .htaccess, but it covers redirect when you visit site explicitly with https://domain.com, but, when visiting http://domain.com, it redirects to http://www.domain.com instead https://www.domain.com.

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Alan Kis
  • 1,790
  • 4
  • 24
  • 47

2 Answers2

1

I personally would lay it out like this, this will force WWW and HTTPs for everything.

RewriteEngine On

#Force WWW on everything
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

#Force HTTPS on everything
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
Joe
  • 4,877
  • 5
  • 30
  • 51
  • If URL is `http://example.com` then this snippet will do two redirects and that is not good for both performance or SEO – anubhava Jul 06 '16 at 16:14
  • Always learning, thanks for your input @anubhava. Will have to change my methods moving forward :P – Joe Jul 06 '16 at 16:21
  • Use https on the first rule and you avoid the double redirect. Never test with [`R=301`](http://stackoverflow.com/a/9204355/1741542). – Olaf Dietsche Jul 06 '16 at 20:14
0

You should replace your existing rule with this rule:

RewriteEngine On

# add www and convert http to https
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

Keep this rule right below RewriteEngine On line and clear your browser cache before testing.

anubhava
  • 761,203
  • 64
  • 569
  • 643