Maintain permalinks moving from new Blogger to WordPress 2.0
Monday, 2. July 2007, 15:37:48
For starters, why doesn't wordpress do it automatically, or as an option ? It does such a good job already in the import code, why not this little final step ? Seems strange to me, especially since I can see the bug report in their problem tracking system.
Anyway, enough whinging about that, here's how to get it to work.
I started with Maintain permalinks moving from Blogger to WordPress from justinsomnia.org.
Sadly the import code - blogger.php is quite different in Wordpress V2.2.1. But fear not, you only need to add just 2 lines of code and you are done.
Open the file
wordpress/wp-admin/import/blogger.phpand look for the line containing the string
wp_insert_post- at around line 520. Insert these 2 lines after the call to wp_insert_post;
preg_match("/([^\/]+)\.html$/", $entry->old_permalink ,$matches);
$wpdb->query("UPDATE $wpdb->posts SET post_name = '" . $matches[1] . "' WHERE ID = '$post_id'");
Upload the file you have edited to your wp-admin/import/ directory.
I like having .html at the end of my URLs, so this method will create URLs on your new site that end in .html. If you don't want that, you can slightly modify the first line as ;
preg_match("/([^\/]+\.html)$/", $entry->old_permalink ,$matches);
$wpdb->query("UPDATE $wpdb->posts SET post_name = '" . $matches[1] . "' WHERE ID = '$post_id'");
How it works: after the new database record has been written for the post itself, we go back into the posts table and write a new value for `post_name'. This value is gleened from a string we got out the permalink name that was read from blogger in the first place. In effect we truncate everything up to the last forward slash in the permalink name from blogger and use that as the post-name (or slug as WP likes to call it) for the new database record.
eg; http://12testing12.blogspot.com/2007/07/an-on-and-other-articles-it-if-then.html became http://newblog.com/an-on-and-other-articles-it-if-then.html whereas it would have otherwise become http://newblog.com/a-an-on-and-other-articles-it-if-then-when.html. You have to look hard, but the first "a" is missing and an extra "when" at the end is missing also.
Blogger will normally drop articles and trucate the URL at 40 characters. Without the ability to exactly match the URI part of the address from the old blog to the new blog one could not use a simple mod_rewrite to redirect hundreds of pages. I gave a poor example, sorry, but it does make a huge difference if you want to keep your pagerank and hair.
I tried this on a blog of 295 posts and so far it has worked a treat. Let me know if it works for you. I haven't proved that the mod_rewrite works yet, but it looks like it should !








dermod # 2. July 2007, 16:36
Worked a treat, thank you so much. :-)
Dermod
David Pascoe # 5. July 2007, 09:30
I've been continuing with the import into WP and so far I haven't found any problems with the blogger import, so hopefully these 2 lines are enough.
davidp.
David Pascoe # 16. July 2007, 01:44
Being able to redirect with just one line in your .htaccess file of course only works if you host your blogger blog on your own site.
Before you do the import over to wordpress, you will need to temporarily change your posting option in blogger to tempname.blogspot.com and import from there. That way you are importing from blogger's database to your wordpress database.
Here is the line from my .htaccess file that does the 301 redirect
RewriteRule ^news/[0-9]{4}/[0-9]{1,2}/([^/]+)\.html http://rosacea-support.org/$1.html [R=301,L]travelhappy # 9. August 2007, 19:05
thanks for this fix. Unfortunately I cannot get it to work with WP 2.2.2 - with the insertion of the extra code, the posts are successfully imported but do not have slugs of their own - the post slug field is completely blank.
I followed Arpit Jacob's tutorial as well and also tried the Blogger.php file he has for download which uses your code but encountered the same problem
http://www.clazh.com/move-from-blogger-to-wordpress-and-maintain-permalinks-and-traffic/
I don't know if I am being thick or if something has changed again in WP. If you can find the time to take a look, I'd appreciate it.
Cheers,
Chris
UPDATE: I've fixed it! I realised that the problem was because my blogger pages were saved with a .php extension. Once I changed .html to .php in your code snippet, it all worked. Phew!
Thanks again for the code.
best
C
David Pascoe # 26. August 2007, 16:54
Anyway another victory for 2 lines of code !!
dirtbikemom2007 # 30. August 2007, 20:21
I already imported my blog from blogger to wordpress. Now I'm trying to fix it so that I can have the same permalinks. I followed your link to justinsomnia. My question is this, should I use the wordpress suicide plugin and delete my posts, then do I follow justinsomnia's directions to change the blogger import file or yours (just adding the 2 lines) Then go back and reimport my posts from blogger? I'm just trying to figure out what exactly I need to do.
I had new blogger, and wordpress 2.2 (or whatever the newest one is)
Thanks so much! (and I'm sorry if this is a stupid question, I just want to make sure I don't need to do all the step that justinsomnia typed out since I have wordpress 2.2)
David Pascoe # 30. August 2007, 23:22
I just read the justinsomnia to see what was out there, and found his version didn't work, so wrote a similar one that worked for wp2.x
dirtbikemom2007 # 30. August 2007, 23:27
(Great blog btw!)
dirtbikemom2007 # 31. August 2007, 00:12
thank-you.
David Pascoe # 31. August 2007, 02:13
BTW for those interested, here is a real example of how it is live and working now.
OLD URL:
http://rosacea.ii.net/news/2005/08/mexoryl-sunscreen-with-good-uva.html
(this URL will redirect - this is what is used to look like - http://web.archive.org/web/20070220062939/http://rosacea.ii.net/news/2005/08/mexoryl-sunscreen-with-good-uva.html)
NEW URL:
http://rosacea-support.org/mexoryl-sunscreen-with-good-uva.html
(the default wordpress name would have included -filtering at the end of the name)
so this should help everyone see how I structured my .htaccess to redirect all my blog style pages.
David Pascoe # 4. September 2007, 04:00
So I have done that, even though the new URLs are on a new domain as well. I guess this means that they will see pages are now missing from the old sitemap and `new ones' have appeared.
I expect Google to take a few weeks to transfer all the PageRank to my new site, so hopefully but updating my old sitemap it will speed this up. Of course I have a new sitemap for the new site also (semi-automatically generated by WP
David Pascoe # 5. September 2007, 02:02
http://www.clazh.com/move-from-blogger-to-wordpress-and-maintain-permalinks-and-traffic/
Rodney Olsen # 23. November 2007, 12:56
I still have to do a lot of homework on moving photos across, importing Haloscan comments and finding a good, cheap host.
David Pascoe # 25. November 2007, 10:37
Genius, I think not - but glad I was that a simple and elegant solution was possible.
I think there are a number of things that will influence your decision to migrate. Good, cheap hosts might be your first problem. It is quite hard to have mysql+php+web server+dns+routing all working all at the same time. So the cheap guys give cheap service probably.
You might find it hard to beat the cost of blogger hosting
Don't forget to consider backups - most hosting solutions won't do backups for you. You need to backup the wordpress directory tree as well as mysql (separately).
I have found that the comment feature of wordpress is more and more full of spam, sadly. I have had to turn on a plugin called WP_HashCash which does a good job, but I'm not sure how many comments I'm losing. Still, comments are far nicer in wp compared to blogger.
If you can't upload a .htaccess file to blogger.com, you will need to create a redirect meta sectioned html file for each page. Perhaps there is an easier way to do this, as this would be awful.
As long as you keep the blogger hosted domain active you could leave the photos there.
Good luck in your homework !
karlgoldfield # 9. February 2008, 20:25
Jon # 20. June 2008, 11:31
So far Wordpress has impressed me.