0

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

3
  • The SSL certificate seems to be in good shape. It scored an "A' in the evaluation by casecurity.ssllabs.com Commented Mar 10, 2016 at 2:02
  • Actually.. your code is not correct Commented Mar 10, 2016 at 2:04
  • Can you be more specific please? Commented Mar 10, 2016 at 2:05

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Boom! And just like that @Panama Jack fixed the problem AND taught me where I went wrong!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.