
Fix ERR_TOO_MANY_REDIRECTS with Hosts File (2026)
Infinite redirect loop (ERR_TOO_MANY_REDIRECTS) after hosts file edits? Fix WordPress siteurl, Nginx X-Forwarded-Proto, www mismatches and 301 caches.
Table of Contents
- How to fix ERR_TOO_MANY_REDIRECTS with the hosts file
- Why redirect loops happen during hosts file overrides
- Trace redirect chains with curl
- Step-by-step solutions
- 1. Fix WordPress configuration for local/staging hosts overrides
- 2. Declare both www and non-www domains in /etc/hosts
- 3. Forward proxy headers in Nginx
- 4. Clear persistent 301 redirect caches in Chrome
ERR_TOO_MANY_REDIRECTS happens when the server targeted by your hosts file sends a redirect response (HTTP 301 or 302) that immediately bounces the browser back to the starting URL. This infinite redirect loop is common when previewing production websites locally or migrating CMS platforms like WordPress.
How to fix ERR_TOO_MANY_REDIRECTS with the hosts file
To fix ERR_TOO_MANY_REDIRECTS with the hosts file, override hardcoded site URLs dynamically in your CMS (such as wp-config.php for WordPress), forward `X-Forwarded-Proto` headers in your reverse proxy, declare both www and non-www variants in /etc/hosts, and clear your browser's persistent 301 redirect cache.
Why redirect loops happen during hosts file overrides
When you map a live domain to a local or staging IP in your hosts file:
https://mysite.com, but you visit http://mysite.com. The CMS forces an HTTPS redirect, which hits an unconfigured local port.Location: https://... header.domain.test to www.domain.test. If www.domain.test is missing from your hosts file, the request drops back to public DNS and loops.Read WordPress redirects to live site and testing WordPress before DNS propagation for more CMS-specific solutions.
Trace redirect chains with curl
Before troubleshooting in the browser, check the redirect headers in your terminal:
curl -IL http://myproject.testExample output showing an infinite loop:
HTTP/1.1 301 Moved Permanently
Location: https://myproject.test/
HTTP/1.1 301 Moved Permanently
Location: http://myproject.test/Step-by-step solutions
1. Fix WordPress configuration for local/staging hosts overrides
In your wp-config.php file, insert these overrides before the require_once ABSPATH . 'wp-settings.php'; line:
// Dynamically match the current request host
define('WP_HOME', (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://') . $_SERVER['HTTP_HOST']);
define('WP_SITEURL', (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://') . $_SERVER['HTTP_HOST']);
// Recognize HTTPS behind local reverse proxies (Nginx, Caddy, Traefik)
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
$_SERVER['HTTPS'] = 'on';
}2. Declare both www and non-www domains in /etc/hosts
If your web server has canonical redirect rules configured in .htaccess or Nginx:
# Recommended configuration in /etc/hosts:
127.0.0.1 myproject.test www.myproject.testIf you omit www, the redirect exits your local machine and queries authoritative DNS servers.
3. Forward proxy headers in Nginx
When using Nginx in front of Docker containers or Node.js services, pass the originating protocol:
server {
listen 80;
server_name myproject.test www.myproject.test;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}4. Clear persistent 301 redirect caches in Chrome
Browsers cache HTTP 301 Moved Permanently responses aggressively. Even after fixing server rules, Chrome may redirect automatically without contacting your server.
To clear cached 301 redirects:
F12 or Cmd+Option+I).Frequently Asked Questions
The destination server enforces a redirection rule (such as HTTP to HTTPS, www to non-www, or CMS base URL) that routes the browser back to a URL pointing to itself.
Use the command curl -IL http://mydomain.test in your terminal. It prints each HTTP status code (301, 302) and the exact Location header without caching anything.
Define WP_HOME and WP_SITEURL dynamically in wp-config.php based on the current host, and ensure the HTTPS server variable is set when behind a reverse proxy.
Yes. If your server redirects to the www subdomain, both the bare domain and www variant must exist in your hosts file to avoid routing to public DNS.
Related Articles
Fix WordPress Redirecting to the Live Site
WordPress redirects to the live site when testing on a new server because the URL is hard-coded in the database. Fix it with the hosts file, no DB edits needed.
Sleezr Team
Developer tools team
ERR_CONNECTION_REFUSED on Hosts File Domains: 6 Fixes (2026)
Custom domain in your hosts file returning ERR_CONNECTION_REFUSED? Fix port mismatches, 0.0.0.0 vs 127.0.0.1 binding, Docker mappings and reverse proxies.
Sleezr Team
Developer tools team
Fix NET::ERR_CERT_COMMON_NAME_INVALID with Hosts File (2026)
Fix NET::ERR_CERT_COMMON_NAME_INVALID when testing local domains in /etc/hosts. Learn mkcert SAN multi-domain certificates, Nginx and HSTS caveats.
Sleezr Team
Developer tools team
Fix EACCES: permission denied on /etc/hosts (2026)
Fix EACCES: permission denied when editing /etc/hosts in Node.js, CLI tools and shell scripts. Learn safe sudo elevation, tee syntax and permissions.
Sleezr Team
Developer tools team
Fix getaddrinfo ENOTFOUND in Docker and Node.js with Hosts File (2026)
Docker container or Node.js app throwing getaddrinfo ENOTFOUND or EAI_AGAIN despite /etc/hosts? Fix Docker network isolation and IPv4 DNS precedence.
Sleezr Team
Developer tools team
SwitchHosts Not Working? Fix Permissions, DNS Cache, and Hosts Conflicts
SwitchHosts not working on Mac or Windows? Fix write permissions, flush DNS, check VPN/DNS blockers, and know when a SwitchHosts alternative like Sleezr is faster.
Sleezr Team