March 25, 2010
mod_rewrite for moving Tumblr away from a custom domain name

Jason asked a really good question:

Quick question, Tumblr Hive:

For the longest time, I’ve been hosting my Tumblr blogging account (jasonpermenter.tumblr.com) elsewhere with a custom domain name (jasonpermenter dot com). Am I right in assuming that if I click the button and revert back to using “jasonpermenter.tumblr.com” in my Tumblr settings, all the previous external links, reblogs, &c, associated with my blog will break?

Anyone have any help/comments/suggestions/advice before I brazenly take jasonpermenter dot com back for other purposes?

For those who don’t know, Tumblr will automatically create a free website for you at YOURNAMEHERE.tumblr.com. You can, if you wish, use your own domain name, such as YOURNAMEHERE.com.

However (and this is the cool part), Tumblr still maintains YOURNAMEHERE.tumblr.com.

My first reaction to Jason’s question was that he could use the Tumblr Backup App currently in beta. The backup app will let you export your entire Tumblr into static files which can be used on any website, which is very cool. However, there are a few problems with this.

Most Tumblr URLs look like this: http://jasonpermenter.tumblr.com/post/337030717/breaking-links. A “secret” about Tumblr URLs, however, is that this is the only part that matters: http://jasonpermenter.tumblr.com/post/337030717/. Everything after the number is just for show. Tumblr calls them “Descriptive” URLs, and they are good for SEO, not to mention being easier to read when you are looking through your web browsing history.

The good news is that you can put anything you want there, and even change them after it is posted, and Tumblr will figure it out. So imagine Jason changed the URL to http://jasonpermenter.tumblr.com/post/337030717/not-breaking-links but I had already linked to http://jasonpermenter.tumblr.com/post/337030717/breaking-links. Tumblr will handle it.

This is a great feature that Tumblr offers, but there are two “catches”:

  1. The Tumblr Backup App currently exports files with paths like this: /posts/337030717.html (note that instead of /post/ they are exporting to /posts/ and they stick a .html on the end of the number).

  2. You don’t know what URLs someone might have linked to. For example, you could conceivably track down all the URLs in your website and setup all the static files that you exported from your Tumblr, but you wouldn’t catch those people who (like me) sometimes trim off the “excess” part of the URL.

The more I thought about it, the more I realized that trying to solve this using static files and URLs on your own domain was stupid idea.

Really, this is one of those situations where a computer should do all the work for you. After all, it’s a simple matter of calculating the proper substitution. In plain English, all we want to do is this:

Whenever anyone goes to http://something.com slash ‘post’ slash ‘series of numbers’ slash ‘optional words here’ automatically redirect them to http://something.TUMBLR.com slash ‘post’ slash ‘series of numbers’ slash ‘optional words here’…

Turns out there’s a feature to do just that, called mod_rewrite. The problem is that it is both incredibly powerful and incredibly complex.

The good news is that once someone figures out the right incantation, it should work for anyone else trying to solve the same problem. In this case the solution appears to be:

RewriteEngine on
RewriteRule ^post/([^/.]+) http://jasonpermenter.tumblr.com/post/$1 [L]

Add that to the /.htaccess file on your server and all of your incoming links should automatically redirect to the proper place.

Please note that I am not a mod_rewrite expert; in fact, I know very little about it. I figured out the above by adapting information from mod_rewrite, a beginner’s guide (with examples) (which I found via Google) and a little trial & error.

That said, I have it setup on this site, and you can test it for yourself using these URLs:

http://luo.ma/post/337030717/breaking-links

http://luo.ma/post/337030717/

http://luo.ma/post/337030717/LOREM-IPSO-FACTO

All of which should redirect you to http://jasonpermenter.com/post/337030717/breaking-links.

If anyone who knows mod_rewrite better would like to suggest improvements to what I have above, please do.

blog comments powered by Disqus