Here’s a sort-of-interesting problem I’ve been working on recently, and which might help prevent an internet stranger or two from pulling their hair out. When you’re moving from one blog to another, how do you set things up so that folks stumbling across your old site are sent to the new one? This came up while Emily and I were working on Brian’s new site. His existing Typepad site had URLs like this:
http://beutler.typepad.com/home/2007/08/rove-the-erudit.html
I knew that I wanted to clean up the formatting of these a little bit — for one thing, the .html would have to go, as I intended to use some PHP in the pages (on a web server with a standard configuration, only files ending in .php or .php5 are processed for PHP). And that “/home/” seemed extraneous, too.
I did try to make the two similar, though, to simplify things. By default Movable Type creates its URLs from the entry’s title, transforming it into a unique identifier called the basename. I altered MT’s default basename length so that it was 15 characters, matching the length of Typepad’s basenames. I set up the new mapping in MT’s Settings/Publishing area, and ended up with new URLs that looked like this:
http://www.brianbeutler.com/2007/08/rove_the_erudit/index.php
The index.php on the end can be left off — if you ask a Linux web server for a directory, it’ll look for “index.php” or “index.html” or “index.htm” and send it back if it finds it. This lets you have slightly tidier-looking URLs. So our new URL is actually:
http://www.brianbeutler.com/2007/08/rove_the_erudit/
With the format settled upon I was able to write some code that sits on every page of Brian’s Typepad site. When a user arrives the page looks at its URL, applies a set of transformations to rewrite it into the new format, and sends the user to the result. Normally you’d want to do this in a server-side script or .htaccess file, but as far as I can tell Typepad doesn’t let its users have that level of control. So I had to put it in some Javascript that I added to Brian’s Typepad template:
<script type="text/javascript">
location.href = location.href.replace(/beutler\.typepad\.com/,'www.brianbeutler.com').replace(/\/home\//,'/').replace(/\.html$/,'/').replace(/\-/g,'_');
</script>
This looks complicated, but it really just does a few things to the URL (whatever the URL may be):
- Replace “beutler.typepad.com” with “www.brianbeutler.com”
- Replace “/home/” with “/”
- Replace the “.html” at the end of the URL with “/”
- Change all hyphens in the URL to underscores
It then sends the browser to the new URL.
Done! All I had to do now is deliver the website, lean back and adopt an attitude of extreme self-satisfaction.
Except… no. I wasn’t done. Everything was actually much, much worse than I thought.
More »