Redirecting a large number of posts to their new WordPress permalinks.

Add this to the VERY top of your theme’s 404 page.
Change http://localhost/wordpress to your default wordpress folder.
This just does posts and possibly pages that end in the id (with or without the trailing url) Categories etc will have to be done elsewhere or by hand.

  1. <?php
  2. //Kinda obvious
  3. $rf = $_SERVER['REQUEST_URI'];
  4. $rf = explode('/',$rf);
  5. $last = count($rf);
  6. //Filter out regular 404 that arn't old urls
  7. if(is_numeric($rf[$last-1])){
  8. $post_id = $rf[$last-1];
  9. }elseif(is_numeric($rf[$last-2])){
  10. $post_id = $rf[$last-2];
  11. }
  12. // No sense loading WP if there isn't an id
  13. if ( $post_id ) {
  14. // Oh fine we will load wordpress
  15. define('WP_USE_THEMES', false);
  16. require('http://localhost/wordpress/wp-blog-header.php');
  17. $post = get_post($post_id);
  18. //Well hope it is the right post. If not, ohwell. We tried. if($post){
  19. //Lets get the new URL
  20. $new_link = get_permalink($post_id);
  21. //We don't want google to get angry with us. header("HTTP/1.1 301 Moved Permanently");
  22. header("Location: $new_link");
  23. }
  24. }
  25. ?>
  26. Use the following to copy and paste the code.