Skip to content

Instantly share code, notes, and snippets.

@alexeckermann
Created June 27, 2011 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexeckermann/1048771 to your computer and use it in GitHub Desktop.
Save alexeckermann/1048771 to your computer and use it in GitHub Desktop.
The 404 RedirectOMatic 3000
$(function(){
// Fade the page, hide the 'Not found' message a little, there's still hope!
$('#title, #page').css('opacity', 0.3);
// If the JSON is taking its time or fail then reset
var timeout = setTimeout(function(){
$('#page, #title').css('opacity', 1);
}, 2000);
// Get the JSON. Using jQuery.
$.getJSON('/js/url_migrations.json?r='+Math.random(), function(data){
var found = false, tracking = '?utm_source=404&utm_medium=redirect&utm_campaign=pagenotfoundredirect';
// I am curious to see the effectiveness of this hence the Analytics flags
$.each(data['redirects'], function(idx, url){
if(url[0] == window.location.pathname || window.location.hash == '#test'){
window.location.replace(url[1] + tracking); found = true; return false;
// Epic win! Lets redirect to the new page
}
});
// So if we have no match and the URL looks Tumblrish then redirect to Tumblr
if(!found && window.location.pathname.match(/^\/post\/[0-9]+/)) {
found = true;
window.location.replace('http://tumblog.alexeckermann.com' + window.location.pathname + tracking);
}
// Cleanup...
if(found) return;
$('#page, #title').css('opacity', 1);
clearTimeout(timeout);
});
});
{
"redirects":
[
[ "/post/2711036222/quick-tip-going-completely-xib-less",
"/tutorials/going-completely-xib-less.html", false ],
"/tutorials/filtering-nsarray-with-nspredicate.html", false ],
[ "/post/957671497/first-steps-with-core-data",
"/tutorials/first-steps-with-core-data.html", false ],
[ "/post/957196387/fine-art-of-uitableviews",
"/tutorials/fine-art-of-uitableview.html", false ],
[ "...old Tumblr URL...",
"... new Jekyll URL...", 'a flag if its external, not implemented' ]
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment