Skip navigation.

Geek is not Dork

Maintain permalinks moving from new Blogger to WordPress 2.0

I felt sure that someone must have already written a web page about how to get the permalinks to transfer from the new Blogger to Wordpress. All I could find was information about Wordpress version 1.

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.php
and 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 !

my Opera Toolbar Part 1wordpress spam killing your akismet ? try WP_HashCash

Comments

dermod 2. July 2007, 16:36

You BEAUT!!!!!

Worked a treat, thank you so much. :-)

Dermod

djp 5. July 2007, 09:30

Hey Great to hear !

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.

djp 16. July 2007, 01:44

I have now redirected all of my self-hosted blogger pages to my new wordpress domain and apart from one page that strangely had a problem with bracket characters it all went fine.

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

Hi David

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

djp 26. August 2007, 16:54

Thanks for checking back here. I was just about to suggest that you were not in fact using the modified blogger import script, because that would be the first thing to check if you get an empty post_name field - you ought to get something.

Anyway another victory for 2 lines of code !!

dirtbikemom2007 30. August 2007, 20:21

Hi-I have a question before I completely destroy my blog.
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)

djp 30. August 2007, 23:22

If you are considering deleting your wordpress import and starting again, then all you will need is my 2 lines, nothing else.

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

Wow-thank you for the quick reply. I will try it now and let you know how I do.

(Great blog btw!)

dirtbikemom2007 31. August 2007, 00:12

WOOHOOO!!! It worked!! You saved me from a sleepless night tonight, thank-you. I have been stressing over this trying to figure this out since I've worked very hard at writing content for my blog and getting the articles to come up in searches. I didn't want it to be all for nothing.
thank-you.

djp 31. August 2007, 02:13

Hey no problem. I too wanted so bad for my permalinks to work properly as I just had to have an easy way to do a permanent redirect from my hosted blogger.com to wordpress.

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.

djp 4. September 2007, 04:00

Just a note for those checking back here. I just saw a note in Google Webmaster Console that I'm redirecting too many URLs. So apparently they don't like it if too many URLs redirect in one go. The instruction was to replace the URLs in the sitemap with the destination address.

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 :smile:

djp 5. September 2007, 02:02

Here is a nice post that goes into some details with screen shots of how to do the actual import.

http://www.clazh.com/move-from-blogger-to-wordpress-and-maintain-permalinks-and-traffic/

RodneyOlsen 23. November 2007, 12:56

I'm strongly considering moving my two thousand and something post Blogger blog to WordPress. This info should be very handy. (I always knew you were a genius.)

I still have to do a lot of homework on moving photos across, importing Haloscan comments and finding a good, cheap host.

djp 25. November 2007, 10:37

Hi Rodney,

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 :wink:

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

I cannot get this to work. I have tried three times, and my address still ends in a number. I added the code in the correct spot and added the maintain feature. What am I doing wrong?

jonbey 20. June 2008, 11:31

I have just used this to transfer my Blogger blog over to Wordpress version 2.5.1. It worked a treat, thank you very much! There were a few posts with different URLs, but I just manually changed these in Wordpress. I did a quick site: http://www.motleyhealth.com/articles/ search in Google to find how my articles were listed, and checked each one, and when they gave a 404 I searched for the post in wordpress and changed the URL.

So far Wordpress has impressed me.

Write a comment

You must be logged in to write a comment. If you're not a registered member, please sign up.