Php redirect not working

Discussion in 'PHP' started by Personaltrainer, Apr 24, 2008.

  1. #1
    Dear DP Members,
    PHP 301 permanent redirect is not working on my site pages.

    The code I am using is
    Header( "HTTP/1.1 301 Moved Permanently" );
    Header( "Location: http://www.personal-trainers.com/find_personal_trainer.php" );

    Could you guys help me to solve this issue.

    Thanks in advance
     
    Personaltrainer, Apr 24, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Try a lower case H.

    
    header( "HTTP/1.1 301 Moved Permanently" );
    header( "Location: http://www.personal-trainers.com/find_personal_trainer.php" ); 
    
    PHP:
    Also, are you getting any error?
     
    jestep, Apr 24, 2008 IP
  3. ooyes

    ooyes Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    maybe you have some whitespace or text printed before the Header redirect
     
    ooyes, Apr 24, 2008 IP
  4. Personaltrainer

    Personaltrainer Peon

    Messages:
    198
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the suggestion, I tried the lower case but the result is the same again. The code was there in the pages and was working fine till recent past. I am not getting any errors. I am getting the old page which I do not want to show.
     
    Personaltrainer, Apr 24, 2008 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    Make absolutely sure there is no white space or linebreak before any php tag on the page.



    Also set error_reporting to All:

    
    error_reporting(E_ALL);
    header( "HTTP/1.1 301 Moved Permanently" );
    header( "Location: http://www.personal-trainers.com/find_personal_trainer.php" );
    
    PHP:
     
    jestep, Apr 24, 2008 IP
  6. Personaltrainer

    Personaltrainer Peon

    Messages:
    198
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    There is absolutely no white space or breaks before the php tags. The code is at the very beginning of the page.
     
    Personaltrainer, Apr 24, 2008 IP
  7. andrew1056

    andrew1056 Peon

    Messages:
    196
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I just tried the code you originally posted. If it was working before ask your host what they changed. I would try putting the redirect at the very bottom of the page. If your host has output buffering turned off and you send the headers and than try to print something you'll get an error.

    The other thing you could try is to just put this code in the page, and than add stuff in slowly to identify where the problem lies. The only reason I can see this not working is printing something after sending the headers... but it could be something else.
     
    andrew1056, Apr 24, 2008 IP
  8. Personaltrainer

    Personaltrainer Peon

    Messages:
    198
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks Andrew.. let me check with the hosting providers. The site was moved to a new server recently. This could well be the reason. Something might have got disturbed during the server. Thanks for the advice.
     
    Personaltrainer, Apr 24, 2008 IP
  9. anjuman

    anjuman Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hi!
    I think you can use JavaScript redirect method in php. The code below works fine-

    <?php

    $redirectURL ="index.php";
    print "<script>";
    print "self.location='".$redirectURL."';";
    print "</script>";

    ?>
     
    anjuman, Apr 24, 2008 IP