Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Chrome says localhost redirected you too many times on a PHP site

ErrorERROR: localhost redirected you too many times

Short answer

The browser followed redirects until it gave up, so something sends the request back to where it came from. Look at the Location header of each response to see the loop. It is usually a PHP redirect that fires on the page it points to, an Apache rewrite rule that runs again, or a session cookie that never sticks.

  • Applies to PHP 8.3 and 8.4, Apache 2.4, Chrome
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the server for the headers of the first response, and read the Location line. Then request that Location the same way. When the target sends you back to the first URL, you have found the loop and which two pages are in it.

    curl -I http://localhost/
  2. 2Match what you see, then jump to its fix

    1. 1Location points to the page that is currently being requested, or two pages redirect to each other (a login page sends logged-out users to itself, for example)A PHP redirect fires on the page it redirects toFix
    2. 2The loop starts after you added or changed a RewriteRule, and every Location header points to a URL that matches the same ruleAn Apache rewrite rule in .htaccess runs again on its own resultFix
    3. 3The loop only happens on pages behind a login, and the browser's cookie list for localhost shows no session cookieThe session cookie is never stored, so the login check fails every timeFix
    4. 4The loop appears only in this browser, and a private window or another browser opens the pageCookies left from an earlier run of the siteFix

What does the error look like?

  • ERROR: localhost redirected you too many times
  • ERR_TOO_MANY_REDIRECTS

Cause 1 of 4

A PHP redirect fires on the page it redirects to

1Check

The manual says header('Location: ...') sends a redirect status (302 unless another 3xx is set) every time it runs. Search the pages in the loop for the redirect and read the condition around it.

grep -rn "header(" --include=*.php . | grep -i "location"

2Fix

Redirect only when the user is not already where they should be, and stop the script right after the header, as the manual's example does with exit.

<?php
// login.php: send logged-in users away, never redirect to itself
session_start();
if (isset($_SESSION['user_id'])) {
    header('Location: /dashboard.php');
    exit;
}
// otherwise show the login form

3Verify

Request the page again and check that the response is a page, not another redirect.

curl -I http://localhost/login.php

Confirmed by php.net

Did this fix it?

Cause 2 of 4

An Apache rewrite rule in .htaccess runs again on its own result

1Check

In per-directory context (.htaccess), the [L] flag stops the current pass but the rewritten request can be processed from the top again. The Apache documentation says this can cause infinite loops.

cat .htaccess

2Fix

Add conditions so the rule skips real files and directories, and end processing with [END]. The Apache documentation shows this front-controller rule.

RewriteEngine On
RewriteCond "%{REQUEST_FILENAME}" !-f
RewriteCond "%{REQUEST_FILENAME}" !-d
RewriteRule "^(.*)$" "/index.php" [END]

3Verify

Reload Apache if you changed the server config, then request the site.

curl -I http://localhost/

Confirmed by httpd.apache.org

Did this fix it?

Cause 3 of 4

The session cookie is never stored, so the login check fails every time

1Check

If session.cookie_secure is on, the PHP manual says sessions only work over HTTPS. On http://localhost the cookie is never sent back, the page sees a logged-out user and redirects to login again.

<?php
// put this in a page and open it through the web server, then find session.cookie_secure
phpinfo();

2Fix

Turn session.cookie_secure off for local development over HTTP, or serve localhost over HTTPS. Keep it on in production.

; php.ini (development only)
session.cookie_secure = 0

3Verify

Restart PHP or Apache, sign in again, and check that a session cookie now appears for localhost.

<?php
phpinfo(); // session.cookie_secure should show 0 or Off

Confirmed by php.net

Did this fix it?

Cause 4 of 4

Cookies left from an earlier run of the site

1Check

Chrome's help page for this error says that pages sometimes fail to open because cookies are not working correctly.

# Open the same URL in a private window. If it loads there, cookies are the cause.

2Fix

Delete the cookies for localhost in Chrome, as the help page suggests, then reload.

# Chrome: delete the cookies for localhost (Chrome Help: delete, allow and manage cookies), then reload

3Verify

The page opens in the normal window as well.

# Reload http://localhost/ in the normal window

Confirmed by support.google.com

Did this fix it?

How do I stop it coming back?

  • Write every redirect with a condition and an exit, and read the condition aloud: when does it stop?
  • Use [END] in .htaccess rules, with conditions for existing files and folders.
  • Keep development and production cookie settings in separate ini files, so a secure-only cookie never meets plain HTTP.
  • Test redirects with curl -I, which shows each Location header without following it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.