localhost redirected you too many times (ERR_TOO_MANY_REDIRECTS)
Fix ERR_TOO_MANY_REDIRECTS on localhost: clear cookies, fix HTTP and HTTPS rules, and WordPress URL settings.
This error means the page keeps sending the browser to another address that sends it back, in a loop. The cause is usually old cookies, or two settings that disagree about http or https, or about www.
What to do, step by step
- Clear cookies and site data for localhost, or test in a private window. A bad cookie can cause the loop by itself.
- Look at the redirects: run curl -IL http://localhost and read the Location lines. You will see the loop.
- Find where each redirect comes from: the web server config (.htaccess, nginx), the app or CMS settings, or a plugin.
- Make sure only one place decides on https and on www. Remove the duplicate rule.
- If you use a proxy or tunnel, pass the original protocol (X-Forwarded-Proto) to the app.
Commands you can copy
curl -IL http://localhostdefine('WP_HOME', 'http://localhost');
define('WP_SITEURL', 'http://localhost');Other causes to check
- WordPress: the home and site URLs differ from how you open it
- Set both in wp-config.php: define('WP_HOME', 'http://localhost'); define('WP_SITEURL', 'http://localhost'); Use the address you really type, with the right port.
- An HTTPS redirect on a site that has no certificate
- On localhost there is usually no certificate. Remove the HTTPS redirect from .htaccess for local use, or set up a local certificate.
- Behind a proxy or Docker
- The app sees plain HTTP and redirects to HTTPS forever. Send the X-Forwarded-Proto header and tell the app to trust it.
- A cache plugin or CDN setting
- Turn the plugin off by renaming its folder, or switch off its redirect option.
Questions about Localhost redirected too many times
- Why does clearing cookies fix it sometimes?
- A cookie that stores a redirect or login state can send the page in circles. Removing it ends the loop.
- Can .htaccess cause it?
- Yes. A rule that redirects to HTTPS, or adds or removes www, fights with the same setting in your app. Keep one of them only.
- What is a redirect loop?
- Address A sends you to B and B sends you back to A, so the browser gives up after about 20 tries and shows this error.
Config generators for this
- nginx HTTPS server blockServe your site over HTTPS with nginx: certificate paths, HTTP to HTTPS redirect, HSTS and a proxy or static site.
- nginx redirect www to non-wwwSend www.example.com to example.com (or the reverse) with one permanent redirect.
- Apache virtual hostHost a website from a folder with Apache, with or without .htaccess.
- .htaccess fileBuild an Apache .htaccess file with an HTTPS redirect, www rules, compression, caching and a front controller.
- docker-compose for WordPressRun WordPress and its database locally in Docker with one command and keep your site between restarts.