Redirect user via 301 - too many loops?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Redirect user via 301 - too many loops?

Post by Celauran »

Exactly as it should be. So we're entering that final conditional block, meaning the problem lies with the header call.

Code: Select all

if ($product->catid != $urlcatid || $product->subid != $urlsubid)
{
    // header("Location: /product/$product->catid/".urlencode($catname)."/$product->subid/".urlencode($subcatname)."/".urlencode($product->id)."/".urlencode($title)."/", TRUE, 301);
    $location = "/product/$product->catid/".urlencode($catname)."/$product->subid/".urlencode($subcatname)."/".urlencode($product->id)."/".urlencode($title)."/";
    var_dump($location); exit;
}
What now?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Redirect user via 301 - too many loops?

Post by simonmlewis »

[text]string(79) "/product/518/CAT-NAMEHERE/344/SUB-NAMEHERE/1919/PRODUCT-TITLEHERE/" [/text]
That produces this, on the page where the URL is wrong.
On the page where the URL is correct, it just loads as it should.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Redirect user via 301 - too many loops?

Post by Celauran »

Does the string output look like what the URI should be? Does that URI work if you request it directly in your browser?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Redirect user via 301 - too many loops?

Post by simonmlewis »

No.
I am putting in a dodgy subid into the URL... that message appears. That message differs from what's in the URL at this point, I suppose because I have commented out the redirection.
So why won't it redirect??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Redirect user via 301 - too many loops?

Post by Celauran »

I asked two separate questions. Which one are you replying no to? Does the URI look wrong? Does the new generated URI look right but not respond when you request it?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Redirect user via 301 - too many loops?

Post by simonmlewis »

Does the URI look wrong?
The URL is what I typed in. It has not changed. So if I type in the wrong one, the wrong one remains. If I type in the right one, the right one remains.

Does the new generated URI look right but not respond when you request it?
If I type in the wrong one, the new one SHOULD generate - but doesn't. This is the issue if I uncomment the header code.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Redirect user via 301 - too many loops?

Post by Celauran »

That doesn't make much sense.

So if you request yoursite.co.uk/product/518/CAT-NAMEHERE/344/SUB-NAMEHERE/1919/PRODUCT-TITLEHERE/ the debug text is:
[text]string(79) "/product/518/CAT-NAMEHERE/344/SUB-NAMEHERE/1919/PRODUCT-TITLEHERE/" [/text]

And if you instead request yoursite.co.uk/product/12345/CAT-NAMEHERE/344/SUB-NAMEHERE/1919/PRODUCT-TITLEHERE/ the debug text is:
[text]string(79) "/product/12345/CAT-NAMEHERE/344/SUB-NAMEHERE/1919/PRODUCT-TITLEHERE/" [/text]

Is that what you're saying? It's pulling junk from the database?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Redirect user via 301 - too many loops?

Post by simonmlewis »

No.
If I go to the CORRECT URL... (the top one you quote), then the page loads just fine and dandy.
If I put in the bad one, with the 12345, it loads that error message.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Redirect user via 301 - too many loops?

Post by Celauran »

So far so good. Does the debug message have the correct product ID or does it still contain the incorrect one?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Redirect user via 301 - too many loops?

Post by simonmlewis »

If I put in the CORRECT URL into the address bar, everything works as you expect with the product page loading.
If I then change that subid to anything else, the URL remains as typed, and that debug message appears, but it appears with the CORRECT URL in it... ie.. it doesn't show the dodgy subid.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Redirect user via 301 - too many loops?

Post by Celauran »

simonmlewis wrote:If I put in the CORRECT URL into the address bar, everything works as you expect with the product page loading.
That has always been the case.
simonmlewis wrote:If I then change that subid to anything else, the URL remains as typed
Good. That's expected behaviour right now.
simonmlewis wrote:and that debug message appears, but it appears with the CORRECT URL in it... ie.. it doesn't show the dodgy subid.
Perfect. So the correct URI is being generated and it's the header() call that's failing. Remove the debug bits, restore the header, add an exit after the header call and let's see what happens.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Redirect user via 301 - too many loops?

Post by simonmlewis »

That's a new one.

I get Firefox's error message:
The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

This problem can sometimes be caused by disabling or refusing to accept cookies.
But the URL did change to the new one.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Redirect user via 301 - too many loops?

Post by Celauran »

With the URL being rewritten correctly, I'd look at your .htaccess rules.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Redirect user via 301 - too many loops?

Post by simonmlewis »

Ok that was my bad. Maybe you can help there too. I put a / on the end, and in my HTACCESS I don't have one, so that killed it.
If I remove that last slash, it works!!
Page redirects to the new one.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Redirect user via 301 - too many loops?

Post by simonmlewis »

Is it because of that missing exit, it wouldn't work before?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply