PHP Redirect Script

Discussion in 'PHP' started by grant.r.brown, Jul 27, 2011.

  1. #1
    Hi, I'm running the following script on a WordPress blog that has changed domains to do a 301 Redirect to the new spot. Here's the code:

    <?php
    
    define( 'BLOCK_LOAD', true );
    require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php' );
    require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php' );
    
    $post_obj = $wp_query->get_queried_object();
    $post_ID = $post_obj->ID;
    $post_title = $post_obj->post_title;
    $post_slug = $post_obj->post_name;
    
    $post_date = $wpdb->get_results("SELECT post_date FROM $wpdb->posts WHERE post_name = '$post_slug'");
    
    list($y, $m, $d) = explode('-', substr($post_date, 0, 9));
    
    $new_url = 'http://www.example.com/TEAM-NAME/'.$y.'/'.$m.'/'.$d.'/'.$post_slug.'/';
    
    Header( "HTTP/1.1 301 Moved Permanently" ); 
    Header( "Location: ".$new_url."" ); 
    
    ?> 
    Code (markup):
    And its outputting to:

    http://www.example.com/TEAM-NAME/Array/team-usa-has-alot-to-look-forward-too/

    Thanks
     
    grant.r.brown, Jul 27, 2011 IP
  2. grant.r.brown

    grant.r.brown Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Fixed. Added this after DB query:

    foreach($post_date as $post_day) {
    $postted_date = $post_day->post_date;
    }
     
    grant.r.brown, Jul 28, 2011 IP