Sleezr
Download
Fix ERR_TOO_MANY_REDIRECTS with Hosts File (2026)

Fix ERR_TOO_MANY_REDIRECTS with Hosts File (2026)

S
Sleezr Team
··3 min read

Infinite redirect loop (ERR_TOO_MANY_REDIRECTS) after hosts file edits? Fix WordPress siteurl, Nginx X-Forwarded-Proto, www mismatches and 301 caches.

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:

1
CMS base URL conflict: The database stores https://mysite.com, but you visit http://mysite.com. The CMS forces an HTTPS redirect, which hits an unconfigured local port.
2
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.
3
Missing www entry in hosts file: The server redirects 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:

BASH
curl -IL http://myproject.test

Example output showing an infinite loop:

TEXT
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:

PHP
// 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:

TEXT
# Recommended configuration in /etc/hosts:
127.0.0.1  myproject.test www.myproject.test

If 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:

NGINX
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:

1
Open Chrome DevTools (F12 or Cmd+Option+I).
2
Go to the Network tab.
3
Check Disable cache.
4
With DevTools open, right-click the browser reload button and select Empty Cache and Hard Reload.
Also readFix WordPress redirects to live site
Also readTest WordPress before DNS propagation
Share this article

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

3 min read
WordPressredirectmigration

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.

S

Sleezr Team

Developer tools team

7 min read
hosts fileERR_CONNECTION_REFUSEDDocker

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.

S

Sleezr Team

Developer tools team

3 min read
Node.jshosts filepermissions

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.

S

Sleezr Team

Developer tools team