0

I have a website with https://example.com, http://example.com, https://www.example.com and https://www.example.com working fine.

Now I want that regardless of which one you type, it redirects to https://www.example.com. I am using the code below in my .htaccess (and tried many more), but after doing the redirection in the browser the page doesn't load, giving error "Too many redirects". I tried of course deleting cache.

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
  • Are you using cloudflare or something similar? – Jon Lin May 30 '18 at 14:02
  • I am using Heroku –  May 30 '18 at 14:09
  • Heroku has its own routing layer in front of your application, so you won't always know whether it came in over SSL or not. This question has a few answers specific to Heroku https://stackoverflow.com/questions/34862065/force-my-heroku-app-to-use-ssl-https – Jon Lin May 30 '18 at 14:14
  • @JonLin That's a little misleading. You will always know whether it came in over SSL or not. It's just not the port number that matters. The `X-Forwarded-Proto` is what tells you, and it should be consistently present. – ceejayoz May 30 '18 at 15:01

2 Answers2

4

Since my hosting is Heroku, which has special rules, any of the other solutions didn't work for me. The following code in .htaccess worked like a charm:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
1

I have something Similar issue the bellow code solve problem for me

 RewriteEngine on
 RewriteCond %{HTTPS} off
 RewriteCond %{HTTP_HOST} ^[DOMAIN HERE]$
 RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Note: Replace[DOMAIN HERE]Should be Replaced With you domain