PHP Redirect Script for Domain Change

Hello,

I’m in the process off transferring a WordPress blog to a WordPress Network site. I’m also changing the permalink structure, so I’ve created the following script to get the posts date because I need part of it for the new permalink structure. Here’s the script I’ve created and the output I’m getting. Any help would be great:

<?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/subdirectory/'.$y.'/'.$m.'/'.$d.'/'.$post_slug.'/';

Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: ".$new_url."" ); 

?>

Output:

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

Are you running Apache? It would be way easier to do it with .htaccess.

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]