After exporting my blogger site and importing, everything was looking great, except one thing. The old site was still getting some (read, very little) traffic. And I wanted to redirect that to my new site. My solution works for humans, not so well for crawlers (that don’t run js). It requires a JS redirect on the blogger site, and a permalink set on the jekyll site.
Redirect (on Blogger)
In the template of the blogger site I inserted the following code into the <head>
.
<script type='text/javascript'>
var path = "<data:blog.url/>";
path = path.split("blogspot.com")[1] || "";
location.href = "http://rharriso.github.io"+path; // replce with your blogs name
</script>
<meta content='5; url=http://rharriso.github.io/' http-equiv='refresh'/>
This will redirect javascript running browsers immediately, and non js browsers to the home page after 5 seconds.
Permalink (on Jekyll)
The redirection works, mostly. The names of the files don’t quite match of if a permalink isn’t set. Thankfully for me they were pretty close.
http://rharriso.github.io/2015/03/19/adding-copyright-info-paperclip.html # want
# vs
http://rharriso.github.io/2015/03/adding-copyright-info-paperclip.html # redirected to
Not too far, but not good enough. I can add a permalink to the front matter of each post, but there are like 60 so I don’t want to do that manually. So I broke out my trust irb REPL and got to work. This turned out the be pretty easy, Chiefly because the content I needed to add easily easy from the contents of the file, and the files I need to adjust had a distinct suffix from the newer post that were written on Jekyll. Below is the quick script I banged out.:
# run in the _posts folder
Dir.glob("*.html").each do |file|
content = File.read(file)
# add line to the front matter, using the original blogspot url
content.gsub!(/^blogger_orig_url: http:\/\/c0d3it\.blogspot\.com([^\n]+)\n/,
"blogger_orig_url: blogger_orig_url\\1\npermalink: \\1\n")
File.write(file, content)
end
And that’s it. Now my old posts redirect to their equivalent on the new blog.