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

Posted on Thursday the 25th of January, 2007 at 10:45 am in WordPress

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.
  19. if($post){
  20. //Lets get the new URL
  21. $new_link = get_permalink($post_id);
  22. //We don't want google to get angry with us.
  23. header("HTTP/1.1 301 Moved Permanently");
  24. header("Location: $new_link");
  25. }
  26. }
  27. ?>
  28. Use the following to copy and paste the code.
  1. jez posted the following on January 26, 2007 at 5:49 am.

    I came from kates website (sideblog) to yours,
    hence you seem to be a genius with sql and php,
    I hope that you might find out what causes this strange behavior with pages and wordpress 2.1 (latest):

    with my wordpress 2.1 and the 3.6er sideblog plugin activated,
    pages are hidden and return a 404,

    if I deactivate the plugin,
    they are right back and show.

    any clue?
    faustina of gollygolly.com has the same issue.

    I found out that pages seem to be different in wp 2.1,
    here is a link describing it more in detail:

    http://www.transycan.net/blogtest/2007/01/23/how-pages-are-different-in-wp-21/

    in case you find a quick fix,
    feel free to share it,
    cheerio!

    Reply to jez
  2. Aaron posted the following on January 26, 2007 at 5:29 pm.

    I’m not going to start supporting other people’s plugins but you can try:

    Find in sideblog.php:

    Replace with:

    I haven’t personally seen this happen on my website, so it may work it may not.

    Reply to Aaron
  3. Jeremiah Squib posted the following on February 1, 2007 at 1:47 am.

    Hey thanks, that worked great.

    Reply to Jeremiah Squib

Leave a reply

:) :D :( :o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: ;) :!: :?: :idea: :arrow: :| :mrgreen: