Redirect old URI’s to new WordPress posts using PHP

In my case I migrated from a custom blog to WordPress. My old one used a GET string in the URL to define which post to load and now I’d like to redirect one and maybe others to their WordPress counterparts.

Using %{QUERY_STRING} in some mod_rewrite mojo might get the job done, but a) mod_rewrite makes my brain melt and b) There is zero reason the server should compare every request looking for something going to the WordPress index page alone.

This will need to be placed at the top of the index.php file used by WordPress. The “301” at the end of the header() function is the HTTP status “moved permanently”. This example is unique to this site but you should be able to define your own from it if you have a similar need.

if ($_GET['focus']) {
  switch ($_GET['focus']):

    case "34":
    $new_loc = "/2007/10/itunes-library-to-mysql-database/";
    break;

  endswitch;
  header("Location:http://".$_SERVER['SERVER_NAME'].$new_loc,301);
  exit;
}

Leave a Reply

Your email address will not be published. Required fields are marked *