skip to Main Content

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>

4

Answers


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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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]
    
    Login or Signup to reply.
  4. 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]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search