Lars Wiegman

  • Home
  • Skills
  • Contact
  • Blog

Personal URL Shortener In Three Lines Of Code

posted on April 23, 2011 #

Having your own URL shortener service has some benefits over using a third party service. To name two: more exposure for your personal domain and full access to web traffic data. But if, for whatever reason, you don’t want to host your own here is a quick hack using the Nginx webserver.

Choose a reliable URL shortener service. I’m betting on Google’s service at http://goo.gl/. Add the following three lines to the server section of your Nginx configuration:

location ~ /u/(.*)$ {
    rewrite ^/u/(.*)/?$ http://goo.gl/$1 redirect;
}

Generate a shortened URL at http://goo.gl/ and replace the goo.gl domain with your own. In my case:

http://goo.gl/nlthH > http://namsral.com/u/nlthH

When users click on your shortened URL they will be redirected to Google’s shortened URL which in turn will redirect users to their final destination. This all happens in a flash, try it .

By adding a unique path to your shortened URLs you’ll be able to differentiate them from other content on your site. Now using grep you can quickly scan through the Nginx logs for clicks:

grep '/u/' /var/log/nginx/namsral.com.access.log

Read Next:

How Steve Jobs Will Be Remembered

Barack Obama, via AllThinsD: And there may be no greater tribute to Steve’s success than the fact that much of the world learned of his passing on a device he invented. Larry Page, CEO at Google, via Goolge+: He was very kind to reach out to me as I became CEO of Google and spend time offering his advice and knowledge even though he was not at all well. Continue »

  • Home
  • Skills
  • Blog
  • Contact
  • •
  • Archive
  • Feed
  • GitHub

Copyright © 2007-2018 Lars Wiegman, lars@ at namsral.com