homepage Welcome to WebmasterWorld Guest from 54.196.77.82
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
301 redirect misbehaving
wheelie34




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

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

 

Morgenhund




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

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!

whoisgregg




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

I always die [php.net] after sending a redirect header... if nothing else to save on CPU cycles.

coopster




msg:3841101
 12:29 pm on Feb 3, 2009 (gmt 0)

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;
?>

wheelie34




msg:3841127
 1:27 pm on Feb 3, 2009 (gmt 0)

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?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved