Search code examples
httphttp-redirectinfinite-loop

Detect infinite HTTP redirect loop on server side


Is there a way to detect infinite HTTP 3xx redirect loops on the server? (Not necessarily the exact moment when the client gives an error, as the number of redirects required for that is client-dependent, but log an error after, say, 10 continuous redirects.)

The redirects come from the application code in a LAMP environment. I can change the code, but don't want to change the URL so adding some sort of counter to it is out of the question. I suppose I could use the session to count the number of redirects and erase that count on each non-redirect request, but that is brittle because the session is shared between several browser windows. Is there another way (such as using some HTTP header which the browser would mirror back)?


Solution

  • There is no 'standard' way.

    You should probably approach it with sessions. Only count urls that redirect, but also keep a timer, because you'd only want to trigger if the redirects happen in a very short timespan.

    Of course, fixing the cause is much better than patching the symptoms.