0

I have tried several different redirect configuration in .htaccess to redirect all traffic to the https non-www URL of my site but can't get https://www.example.com to redirect to https://example.com.

To be clear I want:

    http://example.com
    http://www.example.com
    https://www.example.com

to redirect to

    https://example.com

http to https://example.com works fine

EDIT:

I can't remember the various combinations I used.

This is what I have set at the moment

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] 
</IfModule>
slarge
  • 637
  • 1
  • 7
  • 19
  • Note I have been through several other answered questions on stack overflow and none of the solutions has worked for me. This is affecting Firefox, IE and Safari. Chrome works fine. – slarge Jan 30 '20 at 13:06
  • Why not do it at the hosting level instead? – disinfor Jan 30 '20 at 13:13
  • You should really show us what you actually tried. _“Note I have been through several other answered questions on stack overflow and none of the solutions has worked for me.”_ - well then it would be pointless for people to suggest the usual solutions now again. So what _should_ we suggest now, since you are so unspecific? – 04FS Jan 30 '20 at 13:44
  • _“This is affecting Firefox, IE and Safari. Chrome works fine.”_ - this should not be browser dependent in the first place. Make sure that you are not just getting fooled by older, faulty redirects these browsers might have _cached_ already. – 04FS Jan 30 '20 at 13:45
  • I cleared the cache every time I tried a different redirect setting – slarge Jan 30 '20 at 15:51

4 Answers4

0

Try this:

RewriteEngine On

# match any URL with www and rewrite it to https without the www
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]

# match urls that are non https (without the www)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

From here.

Alex
  • 2,707
  • 4
  • 29
  • 42
0

You can use this :

RewriteEngine on

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

Make sure to clear your browser cache before testing this.

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • Thank you. I would like to better understand your answer. Would you be willing to describe what each line is doing? – chillywilly May 30 '22 at 07:16
0

I had problems on a domain where Safari refused to redirect from http to https using

RewriteCond %{HTTPS} off

I now have this working .htaccess content:

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

# remove www.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
ashley
  • 1
  • 2
0

This is working for me. Try this

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