archive about

Dynamic(*) 404 pages for static blogs

Static blog hosting is great, I’m a big fan, I even wrote my own static blog generator. However, sooner or later, you will have to solve problems that used to be trivial when you had your blog on your own web server, and had scripting capabilities to deal with them, but now they seem a bit harder.

One of them is how you deal with pages that have been moved. For example, I used to host this blog on tumblr. Post URLs looked like blog.vrypan.net/posts/post_id. When I moved to bucket3, I decided to move to a new URL scheme and now the same post is found at blog.vrypan.net/YYYY/MM/DD/post_id (the old post_id).

I host my blog on amazon s3. Until later, S3 allowed you to define a 404 page, but had no way to do redirects (they recently added 301 redirects). But you may have decided to host your site to github, or some other provider os “simple-static-html-hosting”, that doesn;t offer redirects as an option.

I decided to create a 404 page that using javascript will guide the user to the new URL.

The idea is simple. 1. Store the array that maps old URLs to new ones as JSON. 2. Use javascript on the 404 page to determine if the user actually asked for one of the pages that moved, and provide a link to the new URL.

How you create the JSON data file depends on your setup and your needs. I wrote a python script that went through my posts, and used the id and the post date to determine the old and new URL. Your case may be different, for example you may have just a handful of URLs that have moved and you can hardcode them by hand, or you may have changed the base directory of your blog (ex, from /blog/ to /weblog/ or something).

The javascript will probably be something simple, like this (taken from my 404.html)

Example: If you go to an old URL, say http://blog.vrypan.net/post/16306741725/ you will see that you get a link to the new one.

comment on google+

-- (*) not really dynamic, but...