Following server error: ERR_TOO_MANY_REDIRECTS

Did anyone had this issue with servers? Is there a way to fix this?
This seems bad server configuration, but its becoming really hard to find a fix for this issue. :sob:

Best regards.

Where are you seeing this?

Hi @baileyherbert,

I’m getting an issue relative to one of our script, concerning one server.
No matter what i do, the error are always the same. I tried fixing through .htaccess, and with php, but nothing changes :
Probably this is a problem with the server or something with bad configuration.
I only use 2 require for the PHP. On my functions file, I require the access from the config.php which makes the connection to server and database.
If the require from inside the functions.php is removed the frontend works fine, but doesn’t manage to connect to database, so there will be an error there, but this is the only way it displays the frontend.
If i let the require inside the functions.php, then I’ll get that error “ERR_TOO_MANY_REDIRECTS”.
I already requested the information about the server to my client, but he didn’t provide any.
He said that he tried into a MAC environment with MAMP, and he still had that issue.
But we tested and created the script into a windows environment, on WAMP, than tested it back onto a Webserver controled by cPanel, with PHP 5.5+ and latest Apache version. PS: Also the script is ready for PHP 7.0.
Then we tested it back on our MAC environment also using MAMP, and we still had no issues by using our script, but our client still has that issue.
Also and checking their website, i noticed that they had CORS issue, so i think that the issue is really concerned to the bad server configuration. Although I’ve been working hard to find a fix to this issue, I don’t think I am responsible for the bad server configuration, and as my script works fine on several servers this was the first one which had this issue.
What you think?

Best regards.

ERR_TOO_MANY_REDIRECTS is an error on the client/browser, not the server.

The cause is when the server tries to redirect to the same URL over and over, endlessly. For example, let’s say we have this script at


This will cause an infinite redirection loop. After a few seconds Chrome will realize it is infinitely redirecting and will halt the page, with its own error message ‘ERR_TOO_MANY_REDIRECTS’.

Is this issue occurring on other .php pages outside of your script? If not, then it is an issue within your script. If all .php files are causing this, even a basic hello world, then there is likely a misconfiguration on the webserver.

1 Like

But it works fine on other servers and local servers.
Can’t see where’s the problem :\

I have a redirect like this:

 try { $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); }
    catch(PDOException $ex){ echo header('Location: index.php'); die;}

Hummm, now that you say that i created a phpinfo file just for test, and this file works fine, and i can see the server information, but that index.php isn’t displayed… damn redirects… :expressionless:

If index.php also includes that code, then it will cause an infinite loop (and local DOS attack on the database server :stuck_out_tongue: ) if the database details are wrong. When an error occurs you should notify the user of the error instead of redirecting.

try { 
    $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
catch(PDOException $ex) { 
    die("Database credentials are incorrect. {$ex->getMessage()}");

No no, index.php is requiring functions.php. But inside functions.php I require config.php.

Now i think i understood the loop, but I don’t think I have a way to control this, because I need to require / include the files for use :\

Does index.php ever call/require/run the following code?

 try { $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); }
    catch(PDOException $ex){ echo header('Location: index.php'); die;}

If so, that code can cause an infinite loop. Fix it by removing the header function and instead showing the error. It’s really simple.

Well… not directly…
index.php > require functions.php > require config.php

So i understand the loop is caused because config has the echo header(‘Location: index.php’);, so it will loop :\

Yes. Tell your client to check their database details. :slight_smile:

1 Like

I already requested them. Thank you for your help. I’ll try to see if i can fix it.
But why this bug or this redirection happend only in this server and not with others?
I dont get it :sob:

Because only this server has incorrect database details.

1 Like

Thank you so much @baileyherbert.
I’ll let you know if everything worked when i get the credentials.

Best regards.