Jul
11
2005

Redirecting from Blogger to Wordpress

I recently migrated my Dad's blog from Blogger to Wordpress.

To transfer the content, I used Andy Skelton's import tool, which seems to have worked perfectly, especially when combined with Catstutorials' illustrated guide. Many thanks to both of these.

I managed to find a way to get Blogger to redirect to the correct post on the new Wordpress site. Here is how I did it.

First, I made a backup of the Blogger template.

Second, replace the Blogger template with this (you'll need to change this to match your domain):

<html>
<Blogger>
<MainOrArchivePage>
<script language="javascript">
    var blog_root="http://www.yourdomain.com/blog/";
    document.location.href=blog_root;
</script>
</MainOrArchivePage>
<ItemPage>
<script language="javascript">
    var process_page="http://www.yourdomain.com/from_blogger.php";
    var newpage=process_page;
    var oldlink="<$BlogItemPermalinkUrl$>";
    newpage+="?p="+oldlink;
    newpage=newpage.toLowerCase();
    document.location.href=newpage;
</script>
</ItemPage>
</Blogger>
</html>

And then put the following PHP file into www.yourdomain.com/from_blogger.php:

require($_SERVER['DOCUMENT_ROOT'].'/blog/wp-blog-header.php');
$title = $_GET['p'];
$vars = explode('/', $title);
$num = count($vars) - 1;
$filename = $vars[$num];
$slug = str_replace(".html", "", $filename);
$SQL = "SELECT posts.* FROM $wpdb->posts AS posts WHERE posts.post_name = '$slug' LIMIT 1";
$posts = $wpdb->get_results("$SQL"); if ($posts)
    { foreach ($posts as $post) {
        $found_link = = get_permalink($post->ID); }
    } else {
        $found_link = "http://www.yourdomain.com/blog/";
    }
header("Location: $found_link");

It works for me …

Update: thanks to Tom Sherman for correcting my typo. 

Written by Owen in: Aid works, Development, Economics, aid
RSS Feed for Comments

8 Comments so far »

  • rob on November 12, 2005 at 7:28 pm

    hey
    Any chance you could but a few comments in this code. I would just like to get a bit of an overview as to what it is doing and my PHP and javascript knowledge are nonexistant. It would be particularly good to spell out what the main variables and functions do.

    Thanks

    rob

  • myBlog » Migrating from Blogger to Wordpress on March 4, 2006 at 5:56 am

    [...] I borrowed code to handle redirection of specific articles. The referenced snippet just searches the new site for the title of the article from the old site and displays the results. [...]

  • jotsheet on May 19, 2006 at 11:35 pm

    Moving from Blogger to WordPress: Best Practices…

    Best practices for moving a Blogger site to WordPress and redirecting specific entries from the old site to the new site….

  • tom sherman on May 19, 2006 at 11:46 pm

    Thanks for this.. I found it quite useful.  However, there is a vital typo!  You conflate the variables "$title" and "$search_link". I’ve written up a tutorial for moving from Blogger to WordPress and included a much easier to read and typo-fixed version of this script.  Might want to give that a shot. 

  • [...] J’attribus cette section en partie à Owen Barder ( je dis en partie parce que son code avait des problèmes de formattage..j’ai perdu un bon moment à essayer de debugger. Du coup, je ne lui offre pas tous les crédits [...]

  • The Obscurer › Barbarism Begins At Home on April 18, 2007 at 11:57 am

    [...] Owen Barder and Tom Sherman for their scripts that, with a few amendments, even mean that all my old obscurer.blogspot.com permalinks now re-direct to the correct posts on obscurer.co.uk. My life is complete. [...]

  • [...] wanted to thank Owen Barder for his excellent code to redirect your individual posts from blogger to wordpress. Also, thanks to Tom Sherman. I will [...]

  • [...] Redirecting from Blogger to Wordpress @Owen abroad [...]

Please join the discussion

Proud to use WordPress | Some rights reserved © Owen Barder