Chrome says localhost redirected you too many times on a PHP site
ERROR: localhost redirected you too many timesShort 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.
Fix it fast
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/2Match what you see, then jump to its fix
- 1
Location 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
The 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
The 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
The loop appears only in this browser, and a private window or another browser opens the pageCookies left from an earlier run of the siteFix
- 1
What does the error look like?
ERROR: localhost redirected you too many timesERR_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 form3Verify
Request the page again and check that the response is a page, not another redirect.
curl -I http://localhost/login.phpConfirmed by php.net
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 .htaccess2Fix
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
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 = 03Verify
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 OffConfirmed by php.net
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 reload3Verify
The page opens in the normal window as well.
# Reload http://localhost/ in the normal windowConfirmed by support.google.com
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix