3

I'm trying to redirect my site from http to https and from www to non-www at the same time. This code from Andron here works but with a few glitches. I've slightly tweaked it and redirected everything from the non-https to https as well.

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

For the following situations the redirect works flawlessly and redirects to the non-www https version:

domain.com
www.domain.com
www.domain.com/page

However, when I enter the domain.com/page or http://domain.com/page I see only the non-secure http non-www version.

How do I make sure that all URLs redirect to the secure https non-www version of the site?

Community
  • 1
  • 1
hpb
  • 111
  • 1
  • 7

2 Answers2

3

Here is a rule that does both http->https and www removal in single rule:

RewriteEngine On

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

Multiple redirects should better be avoided for SEO purpose.

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

You can use:

RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
Croises
  • 18,570
  • 4
  • 30
  • 47