1


I want to redirect www.example.com or example.com to https://www.example.com

I've tried

   RewriteCond %{HTTPS} off
   RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

But it gives me an error ERR_TOO_MANY_REDIRECTS

I also tried

  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^(example\.com)(:80)? [NC]
  RewriteRule (.*) https://www.example.com/$1 [R=301,L]
  order deny,allow

It works fine for example.com,
but not for www.example.com

AFF
  • 33
  • 1
  • 1
  • 7
  • Possible duplicate of [How to redirect all HTTP requests to HTTPS](https://stackoverflow.com/questions/4083221/how-to-redirect-all-http-requests-to-https) – delboy1978uk Oct 22 '18 at 11:39
  • 1
    How is your SSL cert managed? Is it installed directly on the application server or are you using an SSL proxy? Your first code snippet should not result in a redirect loop, unless you are behind a proxy. The fact that your first rule results in a redirect loop suggests you are behind an SSL proxy. – MrWhite Oct 22 '18 at 12:00
  • HEY, if you are using the aws server then you can use the route53 for this problem. – Raman Kumar Oct 22 '18 at 12:02
  • we will go for Rewrite Engine for this reason. " the element after the domain name. It, too, should be relevant, appropriate, professional, memorable, easy to spell and readable. And for the same reasons: to attract customers and improve in search ranking." so your problem related to domain name . – Raman Kumar Oct 22 '18 at 12:08

1 Answers1

1

I've talked with my hosting provider and they replied :

RewriteEngine On 
RewriteCond %{ENV:HTTPS} !on 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

You may use the following lines to force both HTTPS and WWW :

RewriteEngine On 
RewriteCond %{ENV:HTTPS} !on [OR] 
RewriteCond %{HTTP_HOST} !^www.YOURDOMAIN.COM$ [NC] 
RewriteRule ^(.*)$ https://www.YOURDOMAIN.COM/$1 [L,R=301]

I see 'ENV'. I don't know What it means but it works fine :D

AFF
  • 33
  • 1
  • 1
  • 7
  • 1
    The `ENV` prefix references an _environment_ variable called `HTTPS` instead of the "normal" _server_ variable `HTTPS` that you referenced in your question. The `ENV:HTTPS` is being set by your host and implies your host is using some kind of front-end proxy to manage the SSL cert. – MrWhite Oct 22 '18 at 21:13