skip to Main Content

I have a website and I need the following redirections :

  • www.example.com > example.com
  • http requests redirected to https

I wrote the following rules :

#Redirect http://example.com to https://example.com
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example.com
    RewriteRule ^(.*)$ https://example.com$1 [R=permanent,L]

#Redirect www to non-www
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.example.com
    RewriteRule ^(.*)$ https://example.com$1 [R=permanent,L]

The rules are not working and I’m getting the error : domain.com redirected you too many times.

Does anyone know how to solve that please ?

Thanks

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to the help of Anubhava, I could get a working solution :

    #Redirect http://example.com to https://example.com
        RewriteEngine on
        RewriteCond %{HTTP:X-Forwarded-SSL} =off
        RewriteCond %{HTTP_HOST} ^example.com
        RewriteRule ^(.*)$ https://example.com$1 [R=permanent,L]
    
    #Redirect www to non www
        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^www.example.com
        RewriteRule ^(.*)$ https://example.com$1 [R=permanent,L]
    

  2. Your first rule seems to be the culprit which is not checking for http / https and redirecting to https.

    You may use:

    RewriteEngine On
    
    #Redirect www to non www
    RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
    
    # http to https
    RewriteCond %{HTTP:X-Forwarded-SSL} =off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search