Jul 26, 2008

How to 301 redirect from Blogger to WordPress

As promised in my previous post (301 Meta Refresh Redirects: How Google and Yahoo Sees It), I will show you a script I wrote that will handle 301 redirects from Blogger to WordPress using the meta refresh tag and javascript.

I have tested it on my old blogger (http://dannynsl.blogspot.com) and it seems to have worked fine so far.

There are a few steps you will first need to do.

  1. Install WordPress on your domain. There should be installation tutorials on the website. I haven’t had time yet to do my own installation tutorial.
  2. Import your Blogger posts into WordPress. In your dashboard, go to Manage and then Import.
    Wordpress - Import Blogger
  3. Change your permalink structure. In your dashboard, go to Settings and then Permalinks. The Day and name or Month and name settings should work. However, for my script I think Month and name should be safer.
    Wordpress - Permalinks Settings
  4. Use my javascript code just after the start of the <head> tag in your Blogger layout template. Note: Please make sure you do a backup before making changes. The variables that you need to change are old_root_domain and new_root_domain. This script handles redirection of individual posts, monthly archives and yearly archives. Anything else it will redirect to your homepage.

    When assigning values to old_root_domain and new_root_domain, DO NOT use ‘/’ at the end.

<script type='text/javascript'>
/*
 * Written by Danny Ng (http://www.dannytalk.com/2008/07/26/how-to-301-redirect-from-blogger-to-wordpress/)
 * Free to use and distribute but must keep this comment in place.
 */
var post_regex = /^http:\/\/(www.)?.*\.blogspot\.com\/\d{4}\/\d{2}\//;
var month_archive_regex = /http:\/\/(www.)?.*\.blogspot\.com\/\d{4}_\d{2}_\d{2}_archive.html/;
var year_archive_regex = /updated-min=\d{4}/;
var label_regex = /search\/label\/.+/;
var old_root_domain = 'http://dannynsl.blogspot.com', new_root_domain = 'http://www.dannytalk.com', tag_url = '/tag/', redirect_suffix;

if (post_regex.test(location.href))
	redirect_suffix = (location.href.search(/www/i) == 7) ? location.href.substring(old_root_domain.length+4, location.href.length-5) : location.href.substring(old_root_domain.length, location.href.length-5); // -5 to strip .html
else if (month_archive_regex.test(location.href))
{
	redirect_suffix = (location.href.search(/www/i) == 7) ? location.href.substring(old_root_domain.length+4, location.href.length-16) : location.href.substring(old_root_domain.length, location.href.length-16); // -16 to strip _XX_archive.html
	redirect_suffix = redirect_suffix.replace(/_/g, '/');
}
else if (year_archive_regex.test(location.href))
{
	redirect_suffix = year_archive_regex.exec(location.href).toString();
	redirect_suffix = redirect_suffix.replace(/updated-min=/, '/');
}
else if (label_regex.test(location.href))
{
	redirect_suffix = label_regex.exec(location.href).toString();
	redirect_suffix = tag_url + redirect_suffix.split('/')[2];
}
else
	redirect_suffix = '';

document.write("<meta content='0;URL=" + new_root_domain + redirect_suffix + "' http-equiv='refresh'/>");
</script>

If you want to do a 302 redirect equivalent, just change the number in the meta content from 0 to 1 or 2.

Hope this works for you and please let me know if there are any bug issues.

[edit date=26/07/08]

I just fixed the code to accommodate blog addresses with ‘www’ subdomains. As for redirects for labels, I’ll try to update the code for it tomorrow if I have time.

[/edit]

[edit date=27/07/08]

I’ve added a new regular expression to handle labels and also another variable tag_url that points to your permalink structure for tags or categories.

Wordpress - Category, Tag edit

This setting can be found under Setting, then Permalinks and just scroll right down to the bottom. You will need to edit the variable tag_url to what you’ve sent in the permalinks structure. Make sure you add the ‘/’ at the end of it.

I think by default, WordPress automatically imports posts from Blogger and sets the labels to categories instead of tags. In this case, just set the variable to the category slug name unless you can be bothered to go through all the posts and set them as tags instead.

[/edit]

[edit date=14/02/08]

Just found a bug (thanks to Chris Lee) that the redirection didn’t seem to work in IE7. For some reason I needed to add URL= in the meta content attribute for the redirection to work.

Basically I just changed,

document.write(“<meta content=’0;” + new_root_domain + redirect_suffix + “‘ http-equiv=’refresh’/>”);

to

document.write(“<meta content=’0;URL=” + new_root_domain + redirect_suffix + “‘ http-equiv=’refresh’/>”);

Please let me know if you guys ever come across bugs for this script and I will try my best to fix it as soon as possible :)

Happy Valentines Day everyone!

[/edit]

Tags: , , , , ,

74 Comments

  • What if you just wanted to map your followers that are on blogger from your own blogger directly to your wordpress site instead? I don’t care about blogger posts getting imported to my wordpress blog, i dont want that

    • Hi kirby, do you simply want to redirect all traffic from blogger to your new site (wordpress)? If that’s the case, that’s simple. Let me know if you need help with this.

  • very nice solution man! does this javascript redirect passes link juice?

    • No I don’t believe they do pass link juice as traditionally, search engine crawlers are unable to go crawl javascript links. However, this is probably changing over time as they get smarter.

  • dude pls give me solution..your 301 redirect code refresh my new domain pages also and redirects all pages to home page after few minutes.

    pls help me…

    • If you can show me what you’ve done, I can take a look at it.

Leave a comment

Twitter Updates