Welcome to WebmasterWorld Guest from 54.163.84.199

Forum Moderators: coopster & jatar k

301 redirect misbehaving

   
4:04 pm on Feb 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have setup a php 301 (htaccess won't do it) heres what I am after and doing

if ($variable == "name\'s"){
header ("HTTP/1.1 301 moved permanently");
header ("Location: http://www.example.com/folder/names");
header ("Connection: close");
}

Live headers shows it gets a 301 status but it then bypasses the Location and gets caught in a check that I have to make sure num_rows is 1, so I needed to find how far it's actually getting so I added print "$query"; directly after the db call so I could see whats being passed.

With print "$query"; it all works perfect, the correct data loads and the correct URI is shown in the browser address, so, happy as larry I commented out the print "$query"; and it now gets missed completely, any ideas

EDIT: forgot to say, the Location address IS the same page the that the 301 is on

10:18 pm on Feb 2, 2009 (gmt 0)

10+ Year Member



Try to use telnet to debug 301s:
$ tenet localhost 80
GET /blah.php HTTP/1.1
Host: www.yourhost.com
Connection: close

Connection ends when you press "Enter" twice.

The trick is that telnet shows everything (full response you can observe), whereas when trying to debug in browser you get either "redirected or not" result.

Hope that helps!

10:29 pm on Feb 2, 2009 (gmt 0)

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I always die [php.net] after sending a redirect header... if nothing else to save on CPU cycles.
12:29 pm on Feb 3, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



So do I. The manual page for header [php.net] even has the comment:

<?php 
header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
1:27 pm on Feb 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Great guys, adding exit; seems to have sorted it.

OK I have 3 of these file names I would like to 301, they are all in a switch statement, am I better doing this

switch ( $variable ){
case "name\'s":
header ("HTTP/1.1 301 moved permanently");
header ("Location: http://www.example.com/folder/names");
exit;
break;
case "other\'s":
header ("HTTP/1.1 301 moved permanently");
header ("Location: http://www.example.com/folder/others");
exit;
break;
case "more-other\'s":
header ("HTTP/1.1 301 moved permanently");
header ("Location: http://www.example.com/folder/more-others");
exit;
break;
default:
$variable = "$variable";
}

is the above correct? or should I just run 3 seperate if statements?

 

Featured Threads

Hot Threads This Week

Hot Threads This Month