Reputation: 1172
The following code in my htaccess file is creating a "This webpage has a redirect loop ERR_TOO_MANY_REDIRECTS" error in all browsers I've tested.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [NE,L,R=301]
The code is based on @anubhava's contribution https://stackoverflow.com/a/26914740/1459653
The code seemed to work fine in tests I conducted using http://htaccess.madewithlove.be/
However, once implemented, the error occurred and according to the Redirect Mapping tool at https://varvy.com/tools/redirects/ there are 19 Redirect(s) with a "Final status code: 301" for each of www || www (https) || no www (https)
http://www.example.com
301 redirect
https://example.com/
https://example.com/
301 redirect
https://example.com/
https://example.com/
301 redirect
My intention is to redirect so all permutations (example.com / www.example.com / https://www.example.com / http://example.com) result in being redirected to https://example.com/ (which is how the SSL certificate is issued).
Upvotes: 0
Views: 3453
Reputation: 24448
Your code is not correct. What you meant to do is this,
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [NE,L,R=301]
Specifically you were saying if not www.example.com
redirect to example.com
, so it redirects to example.com
and then it's not www.example.com
so it does it again and so on. Hence the redirect loop.
Upvotes: 2