Filipe
Filipe

Reputation: 113

Error TOO MANY REDIRECTS debbuged

I'm spending a few hours trying to solve this ERR_TOO_MANY_REDIRECTS without success.

It happens when I access my website URL without being logged in. So if I do not have a session started I will be redirect to -> mywebsite.com/site/login or just /login

The PHP code that makes that is below:

if ( isset($_GET['p2k_url']) && $_GET['p2k_url'] ) {

        if ( strstr($_GET['p2k_url'], '/') ) {

            $url = explode('/', $_GET['p2k_url']);

        } else {
            $url[0] = $_GET['p2k_url'];
        }

        if ( !isset($url[1]) || !$url[1] ) {
            $url[1] = 'index';
        }
    }

    if ( $url[0] != 'login' && !$__usuCod ) {
        header('Location: /site/login.php');
        die();
    }

    if ( file_exists(SITE_DIR.DS.'_'.$url[0].DS.$url[1].'.php') ) {

        include SITE_DIR.DS.'_'.$url[0].DS.$url[1].'.php';

    } else {

        if ( file_exists(SITE_DIR.DS.'_'.$url[0].DS.'no404.php') ) {
            include SITE_DIR.DS.'_'.$url[0].DS.'index.php';
        } else {
            include SITE_DIR.DS.'_404'.DS.'index.php';
        }
    }

Right here: if ( $url[0] != 'login' && !$__usuCod ) {

My login.php is just a simple html page with form. Nothing there to worry about.

Although my main .htaccess is below and I think it might have something there:

Options -indexes

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^(.*)$ index.php?p2k_url=$1 [QSA,L]
</IfModule>

In order to debug and find the error, when I run tail -f access_log I get just simple access requests:

127.0.0.1 - - [01/Nov/2016:11:16:28 -0200] "GET /site/login.php HTTP/1.1" 302 -

Nothing coming on error_log tough.

Doing reseraches I see that is a very comum problem on WP - which is not the case here. Any ideas?

Thanks

Upvotes: 1

Views: 2318

Answers (1)

Lee
Lee

Reputation: 10603

So you're redirecting to /site/login.php which your rewrite changes to index.php?p2k_url=site/login.php

$url will be an array of ['site', 'login.php'] (i'd trim($x, '/') the string before exploding to be sure).

Then you test if $url[0] != 'login' which will always be true as [0] is site. You need to test if $url[1] != 'login.php' or trim the file extension from the string first.

if you want to allow /site/login and /login then you just need to work out a rule for that. If /site/xxx is always equal to /xxx then just remove the first array index from $url if $url[0] == 'site' (or normalise your url's properly)

A really useful debugging technique i used in my early days was var_dump or print_r along with die() or exit. Use something like die('xxx') and move it up and down your script to identify where the redirect is occuring, then print out the variables to see what they contain and check your comparing them correctly. It's a crude method, but it works

Upvotes: 1

Related Questions