Juan Pablo Orradre Juan Pablo Orradre - 2 months ago 12
Apache Configuration Question

Can't find .htaccess loop error "Too many redirects"

I can't figure out why when trying to load a webpage for which I modified .htaccess file, the browser returns me the error "Too many redirects".

Can you help me to find inside the file where the mistake is?

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^([www\.]*domain\.com)$ [NC]
RewriteCond %{REQUEST_URI} ^/.+$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^([www\.]*domain\.com)$ [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/web [L,R=301]

Answer

Your error is the first rule:

RewriteCond %{HTTP_HOST} ^([www\.]*domain\.com)$ [NC]
RewriteCond %{REQUEST_URI} ^/.+$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This is saying:

  1. If the host is "www.domain.com" or "domain.com"
  2. And the request isn't just "/"
  3. Then redirect to the same host and request using the "https" scheme

So no matter what request you go to, you will always end up in a loop because of that rule.

Let's say, you go to http://domain.com/:

  1. The host is "domain.com" and the URI is "/"
  2. The second rule redirects the browser to https://domain.com/web
  3. The browser loads the new URL
  4. The host is "domain.com" and the URI is "/web"
  5. The first rule redirects the browser to https://domain.com/web
  6. Repeat starting from step 3

Thus you have your redirect loop.

I have no idea what you're attempting to do with that rule, it really only does one thing and that one thing is to cause a permanent redirect loop.