Php error: ...redirected you too many times

oh brother… I finally posted my website just now… that I have been working on for a while and that I have gotten a lot of help with here on the Sitepoint forum (will post address once it’s working…;~)

I get this error:

[site] redirected you too many times....

now I do not get this error in my localhost… this is very upsetting…

so what is wrong, please…

thank you…

PS: this is a typical redirect code:

if ($ss) {
    header('Location: page1/photos.php?pn=1&ss=pb');
} else if (!$ss) {
    header('Location: page1/photos.php');
}
exit();

Immediately after the call to every header(), try exit(); to prevent the remaining script from being called.

Also set error_reporting(); and check the error log.

2 Likes

thank you…

I do have exit();

so I changed this

if ($ss) {
    header('Location: page1/photos.php?pn=1&ss=pb');
} else if (!$ss) {
    header('Location: page1/photos.php');
}
exit();

to this

if ($ss) {
    header('Location: page1/photos.php?pn=1&ss=pb');
    exit();
} else if (!$ss) {
    header('Location: page1/photos.php');
    exit();
}

same thing…
I do have error reporting… only error I get is what I posted in my OP:

[site] redirected you too many times....

and

ERR_TOO_MANY_REDIRECTS

oh brother… this is so upsetting… now why would this occur on the remote server and not in my localhost???

thank you…

here http://www.php.net/manual/en/function.header.php

it shows implementation of “Location:” header…

according to this my code is ok…

What is assigned to $ss? Such as what value is in it?

it’s a param in the url query string

as in…

......../section3/photos.php?pn=2&ss=pb

but I have other situations/conditionals where I apply redirects, for example…

if (!$_SERVER['HTTP_REFERER']) {
   header('Location: contact.php?section=section1&pn=1&wt=600');
   exit();
}

and

if (endsWith($URL,"contact.php") || endsWith($URL,"contact")) {
   header('Location: contact.php?section=section1&pn=1&wt=600');
   exit();
}

basically they all have to do with what’s in the url…

thank you…

I wonder if some of the functions mentioned here would work…

(this post is from 7 years ago…)

Maybe it would be worth the effort to remove or at least limit the number of PHP redirects and have the htaccess file do the work?

Are you trying to redirect the page back to itself? Because that’s most likely where it’s going wrong. If you redirect the same file back to itself, it won’t know what to process. Only the bit of condition you are asking it to which is redirect to itself and/or redirect to a different source which also redirects back to itself. Thus, causing an infinite loop because it can’t go any where.

I partially know what you are doing and I partially don’t. I know that you want to redirect the page if it contains a certain phrase or word, but are you suggesting that if the URL contains itself, that you should redirect it back to itself? See where the infinite loop comes in?

If the user is already on the page, you should validate to see if the page contains the right GET parameters. If it doesn’t, then you would redirect the user to the appropriate page or display a 404 error page.

That’s what I would suggest because they came from an invalid source.

back to itself? like

if (endsWith($URL,"contact.php") || endsWith($URL,"contact")) {
    header('Location: contact.php?section=section1&pn=1&wt=600');
    exit();
}

(what other way is there to add a query string to the current url? ;~)))

and again: this is not causing any probs in localhost…

and: thank you very much for your help…

Most likely because you don’t have error log enabled on localhost or you aren’t checking the error log. If you are using a live server and they provide an error log for you and it’s enabled. That’s most likely why you aren’t seeing any errors in your localhost error log. Because it’s not enabled.


Live servers aren’t that different from localhost. The only difference is, some development features are disabled on live servers and production features are disabled on localhost.

They should mirror one another or have very similar configurations.

1 Like

wait – this problem does not occur in the localhost b/c I don’t have error logs enabled?

well, then I guess I shouldn’t have the error logs enabled in the remote server…;~)

(don’t know how to find error logs either on localhost or in remote server… am new to PHP website… used to have Tomcat/Java… error logs were very easy to find, both locally & on remote server…)

What are you using first of all? Are you using XAMPP, WAMP, MAMP, or a custom method?

I’m on a mac, it’s an Apache server… it came with the mac out of the box…

I didn’t even have to install anything, just do some configs that I looked up online… it works like a charm… I don’t even have to turn the server on after restarting the computer… (in that sense it’s much simpler than Tomcat… but redirects are much simpler in Java/JSP; I never ever had a problem with redirects in JSP…)

I still don’t get why this is causing a problem only in the remote server…

thank you…

It’s causing a problem because since you didn’t debug it locally, when you put it online, it will show symptoms of broken code. Sometimes, it could be the live server’s fault, but most of the time, it’s the developer. The reason why one should have error logs enabled both locally and live is because, you can then debug the error if it is the same one. Sometimes, symptoms from live servers should tell you that either you are doing something wrong or your live server doesn’t allow you to use certain functions which isn’t this case.

There seems to be an infinite loop causing your [site] redirected you too many times....

Just a quick Google search already tells me that I am on the right case. Just read this Stack Overflow post.

Another article.

And again, but with $_SESSION

http://stackoverflow.com/questions/11441650/php-the-website-has-too-many-redirects-when-use-php-sessions-with-time

And again

http://stackoverflow.com/questions/39379866/redirected-too-many-times-error-php-mysql


Just using the key words php redirected you too many times in the Google search box gives me all these fantastic results which basically comes back to my first post of this topic.

An infinite loop


Basically, that’s why I was suspicious with your $ss variable. Also,

} else if (!$ss) {

won’t do anything because it’s basically saying

If the variable $ss is (not?), redirect it to this page.

However, the variable $ss does exist because you’ve declared it manually.

thank you… yes I have also searched a lot and seen some of the same SO threads…

but I still don’t see a solution… how do you do redirects in PHP, then? I mean if you redirect to the same page it’s like reloading the page, no? why would this be a problem? this is never a problem in Java…

and again, how come it works fine in the localhost?

if it works in the localhost it means there will appear no errors in the local error logs… yes???

PS: I have this in my PHP code:

error_reporting(E_ALL ^ E_NOTICE);

this should display errors, yes?

one of the SO threads one response is to add

ob_start();

at the beginning of the PHP code… I did that, it didn’t work…

thank you…

Is this code above the current file in question? Or is it just in 1 file? You should place it in all files.


As for your first question. No, it only works in localhost when you turn off error logs. When you don’t have error logs enabled, it won’t complain about anything. It will ignore it as if it was executing it like normal.

The simplest way is to start with a fresh file. Try to imitate the same code, but use different wording and logic. Sometimes, the logic may also not work.

} else if (!$ss) {

Is a clear example.


I’ll post a few sample snippets for you to try out in a little bit. I will test them first and provide working snippets.

it’s at the very beginning of every page…

but I don’t understanding this error-log issue…

if errors logs are “turned off” (I have never “turned off” error logs…)

then, even if there’s something wrong with the code, that code will still work b/c the error logs are turned off??

this I don’t get…

thank you…

May we see more than just that small portion then? It would help a lot to determine what the problem may actually come from.

From my assumption, it’s the logic and $ss that’s causing it.