
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.
Manage hosts files without the terminal
ToggleHosts helps you manage environments visually on Windows, macOS, and Linux, with automatic DNS flush and backups.
One-time payment
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:
- CMS base URL conflict: The database stores
https://mysite.com, but you visithttp://mysite.com. The CMS forces an HTTPS redirect, which hits an unconfigured local port. - Asymmetric SSL termination on reverse proxy: The reverse proxy terminates HTTPS and talks to your app container over HTTP. The application assumes the connection is insecure and issues another
Location: https://...header. - Missing www entry in hosts file: The server redirects
domain.testtowww.domain.test. Ifwww.domain.testis 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:
- Open Chrome DevTools (
F12orCmd+Option+I). - Go to the Network tab.
- Check Disable cache.
- With DevTools open, right-click the browser reload button and select Empty Cache and Hard Reload.
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 SiteWordPress
Fix WordPress Redirecting to the Live Site
3 min read
Fix NET::ERR_CERT_COMMON_NAME_INVALID with Hosts File (2026)Troubleshooting
Fix NET::ERR_CERT_COMMON_NAME_INVALID with Hosts File (2026)
4 min read
Fix EACCES: permission denied on /etc/hosts (2026)Troubleshooting
Fix EACCES: permission denied on /etc/hosts (2026)
3 min read