Skip to content
FunnelGuard

Fix guide by FunnelsGuard

Infinite redirect loop caused by JavaScript: how to find and fix it

PRF-132High impactDetectedBy FunnelGuardPublished Updated
On this page

What this issue is

An infinite redirect loop is when a page keeps sending the browser to another page, and that page sends it back or to a third page that leads to the first. Server redirects (301 or 302) can loop, but this guide covers loops started by JavaScript, such as window.location.href = ..., location.replace(...) or a meta refresh added by a script.

Typical causes of JS redirect loops
  • A geo or language redirect that does not check whether the visitor is already on the right version
  • A login or consent check that redirects to a page which runs the same check
  • A www / non-www or http / https fix in JavaScript that fights a server rule
  • A redirect that depends on a cookie or localStorage value the browser blocks, so it never remembers it already ran
  • An A/B testing or personalisation script that sends visitors to a variant, and the variant sends them back
  • A mobile or desktop redirect script that disagrees with a responsive page

Why it costs you leads or sales

Where a redirect loop cuts the funnel
  1. Ad clickVisitor arrives from an ad or search result
  2. Landing pageScript redirects to /region and back, again and again
  3. Form or productNever reached
  4. Lead or saleNever recorded

The visitor never gets past the landing page, so every later step is lost.

Visitors reaching the formExample numbers
Visitors reaching the form (example)No loop: 60 visitors per 100; Loop for some visitors: 42 visitors per 100; Loop for everyone: 0 visitors per 1000234660420No loopLoop for some visitorsLoop for everyonevisitors per 100

Example: if a loop affects visitors with cookies blocked, those visitors never reach the form. Numbers are illustrative.

How to check if your site has it

Check for a JavaScript redirect loop
  1. Open the page in a private window

    Use a fresh private or incognito window so no cookies or saved values hide the problem. Watch whether the page reloads repeatedly or the address bar keeps changing.

  2. Open DevTools before loading

    Press F12, go to the Network tab and tick Preserve log. Load the page and look for the same URLs repeating. A document request followed by another document request to a different URL is a sign.

  3. Test the variations

    Try with and without www, http and https, with your ad URL parameters (utm_source, gclid), with cookies blocked, on mobile, and with a VPN or browser language set to another region.

  4. Find the script that redirects

    In the Sources tab, search all files (Ctrl+Shift+F or Cmd+Option+F) for window.location, location.href, location.replace and location.assign. In the Network tab, the Initiator column shows which script triggered each navigation.

  5. Check tag manager and plugins

    Look in Google Tag Manager for custom HTML tags with redirect code, and in your A/B testing, geo or language tools for redirect rules.

How to fix it

Looping redirect (before)javascript
// Runs on every page, including the page it sends people to
if (userCountry === 'DE') {
  window.location.href = '/de/';
}
Safe redirect (after)javascript
(function () {
  var target = '/de/';
  var alreadyThere = window.location.pathname.indexOf(target) === 0;
  var alreadyDecided = false;
  try { alreadyDecided = sessionStorage.getItem('geo_redirected') === '1'; } catch (e) { alreadyDecided = true; }

  if (userCountry === 'DE' && !alreadyThere && !alreadyDecided) {
    try { sessionStorage.setItem('geo_redirected', '1'); } catch (e) {}
    window.location.replace(target + window.location.search);
  }
})();
Where to remove or change the redirect
  1. Find the source

    Deactivate redirect, language, geo and A/B plugins one at a time and reload in a private window. Also check Settings > General: the WordPress Address and Site Address must match each other and use the same https and www form.

  2. Check theme and header scripts

    Look in Appearance > Theme File Editor (or your header and footer scripts plugin) for window.location code and add the safeguards shown above.

How to confirm it's fixed

Confirmation checks
  • Load the page in a fresh private window and confirm it settles on one URL
  • Repeat with cookies and site data blocked
  • Repeat with your ad URL parameters and check they are still in the final address
  • Test mobile, desktop and at least two browsers
  • Test from another region or with a different browser language if you redirect by location
  • In the Network tab, confirm there are no repeated document requests
  • Confirm the form or checkout step loads and works

How to prevent it happening again

Prevention habits
  • Every JavaScript redirect checks where the visitor already is
  • Remember the decision for the session and do not redirect when storage is blocked
  • Keep one owner for each redirect rule so server, plugin and script rules do not fight
  • Test redirects with a clean browser after any change to domains, https, languages or A/B tests
  • Review tag manager changes in Preview mode before publishing
  • Monitor your key landing pages on a schedule

How FunnelGuard handles this

Questions people ask

Why does my page keep reloading and redirecting?

A script on the page is probably sending the browser to a URL, and the page at that URL runs the same script again. Check the Network tab's Initiator column and search your code, plugins and tag manager for window.location, location.replace or location.assign.

What causes a JavaScript redirect loop?

The usual causes are a geo or language redirect with no check for the current page, a login or consent check that redirects to a page running the same check, and a redirect that relies on a cookie or storage value the browser blocks. Server rules that fight a script can also cause it.

How do I stop a JavaScript redirect loop?

Make the redirect conditional. Check that the visitor is not already on the target URL, store that the redirect happened (for example in sessionStorage), and skip the redirect if storage is unavailable.

Is a JavaScript redirect loop the same as ERR_TOO_MANY_REDIRECTS?

Not always. ERR_TOO_MANY_REDIRECTS is shown for server-side redirect chains. JavaScript loops may give no error at all, just a flashing, reloading or frozen page, so they are easier to miss.

Why does the redirect loop only happen for some visitors?

Redirects often depend on country, language, device, cookies or URL parameters. A visitor who blocks cookies, or arrives from another region or with certain parameters, can hit a loop you never see yourself.

Does a redirect loop affect ad tracking?

Yes. If the page never settles, tracking tags may fire many times or never, and click IDs and UTM parameters can be lost along the way. Keep the query string when you redirect.

Can FunnelGuard detect a redirect loop?

Yes. Its scheduled robot visits check for timeouts and redirects, and a loop that stops the journey is flagged after a second-region re-check. It may miss loops that only affect certain countries, browsers or cookie settings.

Does FunnelGuard catch this?

Detected

FunnelGuard checks for this today.

How: run timeout/redirects.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.