Ben Summers’ blog

Implementing a simple URL shortener with Apache’s mod_rewrite

While there are lots of problems with URL shorteners, they do appear to be here to stay. The popularity of long meaningful URLs (which are thought to prompt search engines to rank the pages higher) combined with the popularity of messaging services like Twitter (which restrict the number of characters) means there’s a need for these short URLs.

A reasonable solution is to run your own shortener for your web site, and hope people use it. For this blog, I’m using a few lines of Apache configuration and a script which generates a mapping file. This is how it’s done using mod_rewrite:

  RewriteEngine on
  RewriteMap shortcodes txt:/web/bens/shortcodes.txt
  RewriteRule ^/(s\d+)$ ${shortcodes:$1} [R=permanent,L]

This goes in the VirtualHost declaraction in httpd.conf, or an .htaccess file.

The first line enables mod_rewrite. The second declares the mapping file, which maps the short codes to URLs. The third line matches all URLs which start with s and are followed only by digits. When there’s a match, it looks up the real URL in the mapping file, and returns a 301 permanent redirect response. The L option makes sure no other rules run.

It’s important to use a 301 response rather than the default 302, so that any automated system which resolves the URL knows that the short URL really refers to the long URL and it shouldn’t store the short URL.

The shortcodes.txt mapping file is very simple. Each line specifies a mapping with the short code, a tab character, and then the mapped URL, which can either be a URL on the same site starting with /, or a full URL. The order of the entries in the file is unimportant.

Here’s an example:

  s2    /2009/going-off-the-rails
  s15   /2009/iphone-auto-rotate
  s22   /2009/apache-url-shortener

So will redirect to this blog entry, and redirects to my company’s web site.

You don’t need to restart Apache when you change the file. Every time it looks up a short code, it’ll check the timestamp of the file and reload the contents if it has changed. (If the file gets very large, you could use a dbm file or an external lookup program.)

All that’s left is to create the mapping file, and as in all the good maths textbooks, it’s left as an exercise to the reader.



blog comments powered by Disqus


Hello, I’m Ben.

I’m the Technical Director of Haplo Services, an open source platform for information management.


About this blog


Twitter: @bensummers



Jobs at Haplo
Come and work with me!