• ryy705

    (@ryy705)


    Hello,
    I am trying to do a simple php redirect by putting the following code at the very top of my header.php file. But I get a warning saying that output has been started at line 2. Which is the line that says <?php. I don’t understand why this is happening. I am not echoing anything. Is WP loop causing this? should I make changes? Please help.

    <?php if(have_posts()) : while(have_posts()) : the_post();?>
     <?php
      $externalLink = get_post_meta($post->ID,'link',TRUE);
      if($externalLink) {
       header("Location: $externalLink");
      }
     ?>
    <?php endwhile; endif;?>
Viewing 1 replies (of 1 total)
  • prtscreen

    (@prtscreen)

    To redirect you have to put this:

    <?php
    header("Location: http://www.example.com/"); /* Redirect browser */
    /* Make sure that code below does not get executed when we redirect. */
    exit;
    ?>

    AT THE TOP. That is BEFORE any other code.
    (read more here: http://php.net/manual/en/function.header.php)

Viewing 1 replies (of 1 total)

The topic ‘How can I do a php redirect?’ is closed to new replies.