Fork me on GitHub
09 April 2013


Well, we’ve all been there, we’ve written a site, and one page is getting significantly more traffic than another, but several years after creating it you want to rewrite how your site works, but people still want the old content.

On my old site I wrote an article about Magento Developement, and that drew the bulk of my traffic. Which was odd because I never wrote a part 2 (well, I never published a part 2 anyway) so I didn’t quite get that. “Oh Well..” I thought, whilst I merrily started rebuilding my site to use Jekyll on GitHub pages, not really caring that I’d lose this chunk of traffic.

This morning I realised that I was now giving people a lot of 404 messages because of not considering this, and my Google Analytics told me just how many people were hitting the dead link and then not continuing. This is not what I wanted, I want people to read my site and suchlike.


I found a number of articles that talked about creating redirects to handle things like this, however they practically all required me to put in a plugin. As I’m sure you’re aware GitHub Pages doesn’t allow custom plugins outside of the default Jekyll set to be run on their servers. This is completley understandable, but is a bit of an arse. This meant that there was no clear and easy way to proceed on this front.


However, all was not lost, I hit upon an idea. It’s hardly a perfect idea, because it doesn’t send the appropriate header codes to search engines to notify them of the redirect, but it does at least mean that users can see the article they were looking for.

YAML Front Matter

Within the YAML Front Matter of the post that I was writing I added the link as it was on the old site as the parameter oldurl:

 1 ---
 2 layout: post
 3 category : magento
 4 title : "Magento Development Guide - Part 1"
 5 tagline: "Conciliation"
 6 tags : [tutorial, magento, php, best practices]
 7 summary: "Basic concepts a good magento developer should follow"
 8 image: /assets/images/posts/magento-part-1/header.png
 9 oldurl: /2011/02/a-guide-to-magento-development-part-1-conciliation/
10 ---

404 Page

Within the 404.html page I added a post iterator and a bit of javascript to actually do the redirect:

 1 <script type="text/javascript">
 2   var forwardLinks = []
 3   {% for post in site.posts %}	
 4   	{% if post.oldurl %}
 5   	  forwardLinks.push({ "old":"{{post.oldurl}}", "new":"{{post.url}}" })
 6   	{% endif %}
 7   {% endfor %}
 8   jQuery(function($)
 9   {
10     //get the current pathname
11     var currentPath = window.location.pathname
13     //compare to the list
14     for(var key in forwardLinks){
15       var link = forwardLinks[key]
16       if(link.old == currentPath){
17         //set the redirect
18         setTimeout("window.location.replace('""')", 5000)
19         return
20       }
21     }
22   })
23 </script>

This script simply loaded the redirects into an array, and then used javascript to push the user to the appropriate page depending on what the URL was that caused the 404.


This is far from an ideal solution, but it’s the best I could come up with that would allow me to keep using the Jekyll generation that sits within GitHub Pages.

Related Posts They might not be good...



blog comments powered by Disqus