Daniel Doubrovkine bio photo

Daniel Doubrovkine

aka dB., @awscloud, former CTO @artsy, +@vestris, NYC

Email Twitter LinkedIn Github Strava
Creative Commons License

I’ve recently moved this blog to Jekyll, moved all my art-related posts to a new art.dblock.org Jekyll blog and replaced www.dblock.org with a landing page. The last bit is to redirect any old popular content from the old www.dblock.org, such as Your Commute is Killing You: Move to New York or How To Frame and Hang Very Big Drawings to one or the other site.

The landing www.dblock.org site is also a Jekyll site.

Include the Redirect Gem

Include the jekyll-redirect-from gem in _config.yml.

gems:
  - jekyll-redirect-from

Create Redirect Pages

The jekyll-redirect-from library supports redirect_from and redirect_to. It creates a file for the source path and a page with a redirect to the destination. Since anything that is not prefixed by an underscore is copied as is to the Jekyll _site folder, we can just create a file for each URL in a new redirect directory. For example, here’s my redirect/your-commute-is-killing-you-move-to-new-york.markdown file.

---
redirect_from: /your-commute-is-killing-you-move-to-new-york/
redirect_to: https://code.dblock.org/2011/05/29/dear-california-engineer-your-commute-is-killing-you-move-to-new-york.html
---

Moving Pages with Parameters

My old blog had a more complicated scenario with URLs that included a query string ID in the form of ShowPost.aspx?id=X. How does Jekyll handle .aspx extensions and how do we translate such an URL into an existing post?

The export tool I wrote added a field to every post called dblog_post_id.

---
layout: post
title: "Pure Java Waffle"
...
dblog_post_id: 95
---

We can create a plain HTML page that will extract the ID from the query string and use a map to perform a redirect. Jekyll will not serve .aspx pages, but if you try to retrieve a folder without a trailing slash, you will be redirected to its index.html page. This is part of the HTTP standard. I created posts/ShowPost.aspx.html that iterates over all posts and performs the redirect.

---
permalink: '/ShowPost.aspx/'
---

<script language='javascript'>
var posts_map = {};
{% for page in site.posts %}
  {% if page.dblog_post_id %}
    posts_map[{{ page.dblog_post_id }}] = '{{ page.url }}'
  {% endif %}
{% endfor %}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

var id = getParameterByName('id');
var url = posts_map[id];

if (url) {
  window.location.replace(url);
} else {
  window.location = '/'
}
</script>

Try It

Try www.dblock.org/your-commute-is-killing-you-move-to-new-york and code.dblock.org/ShowPost.aspx?id=103.

Everything is open-source. Click here for the source code for the plain redirect and here for the more complicated ASPX one.