User Tools

Site Tools


tutorials:library:software:shorturl.html

This is an old revision of the document!


URL Shortening

If you hang out on Twitter, you'll quickly get used to seeing tweets that use a URL shortening service like t.co or bit.ly or tinyurl.com. These are basically redirector sites. Say if you have a really long URL, it may take up more space than you have available with a 140-character SMS service.

Of course, if you're a big company like google or facebook, you may get your own shortener that hints at the original url, like goo.gl or fb.me. We post a bunch of links to twitter all the time, and we sometimes put URL's on stuff so we thought it would be pretty cool to have our own shortener. That's when we realized, hey we could get a .it (Italian) domain and have our shortener be adafru.it! We quickly registered it, and set out to install our own shortener.

We use our URL shortener for a few things. One is of course for twittered URLs. For example, if we twitter about a blog post, the format is adafru.it/bnnnn where nnnn is a blog post number, for example http://adafru.it/b16184 is a link to blog posting #16201.

We also use it on our packaging. To improve shipping and inventory accuracy, we now barcode many of our items. And since we have a little space on there, why not also stick a tiny url to the product page? That way customers can immediately remember what it is they ordered, and look up tutorials and specifications. Handy! This format is adafru.it/nn where nn is the product #. For example this FTDI cable is adafru.it/70

shortypkg.jpg

Finally, another really great thing about URL shorteners is they automatically include statistics management. So if you're wondering how many people really did follow that link, or click on that tweet, its really easy to get hard metrics, and its not as ugly as an 'affiliate' style URL!

We decided instead of rolling our own we would use a great open source package called YOURLS. Tough to pronounce, but easy to use!

YOURLS

We use yourls for url shortening, its awesome. It has hooks, API calls, and everything else you need and nothing you dont.

The download/install instructions here work great out of the box - just follow the directions and it works like advertised.

Yourls + Wordpress + Twitter

We recommend you use the Wordpress Plugin if you use Wordpress and Twitter. This Wordpress plugin will automatically shorten every blog post you make and tweet them. You will have to register your blog as a Twitter application, which isn't hard.

This plugin will NOT create short URLs for blog posts that you've made in the past - if you want to do that automatically you'l have to write your own plugin.

Yourls + Twitterfeed

Alternately you can use a service like Twitterfeed which can do the exact same thing! To configure Twitterfeed to use your own URL shortener when tweeting things, you need to set a custom endpoint.

Go to Edit Feed > Advanced Settings and set the field "Custom endpoint" to

http://example.com/yourls-api.php?signature=123456789&action=shorturl&format=simple&url=%@

where example.com is wherever you put your yourls, and 123456789 is the API signature that can be found in the admin of our own yourls install.

twitterfeed.jpg

Yourls + ZenCart

It's pretty nice to make shortened URLs for product pages (adafru.it/51 goes to adafruit.com/products/51). This is trivial and you could enter them in by hand, or you could write a plugin that does it for you automatically.

Making a Yourls plugin

Yourls has really nice plugin system with lots of hooks and filters everywhere to make ti easy. If you've ever written a Wordpress plugin then you'll feel right at home here.

First, create a folder under /user/plugins/ and call it my_plugin.

Create a file in /user/plugins/my_plugin/ called plugin.php

The file should look like this

<?php
// Create a new entry if this is a blog post or a product                                                                                                                                                                                                                     
yourls_add_filter('get_keyword_infos', 'create_if_empty');
 
function create_if_empty($infos, $keyword)
{
  global $ydb;
 
  $redirect_new = false;
  if($infos === FALSE && (int)$keyword > 0 && (int)$keyword == $keyword && $keyword < 1000) // Change 1000 to a rational upper limit of your product numbers.
    {
      $url = 'http://www.my_zencart.com/index.php?main_page=product_info&products_id=' . $keyword;
      // create new link                                                                                                                                                                                                                                              
      yourls_add_new_link( $url , $keyword, "pid: " . $keyword);
 
      // switch $infos to the newly generated link
      $table = YOURLS_DB_TABLE_URL;
      $infos = $ydb->get_row("SELECT * FROM `$table` WHERE `keyword` = '$keyword'");
 
      if( $infos ) {
        $infos = (array)$infos;
	$ydb->infos[$keyword] = $infos;
      } else {
        $ydb->infos[$keyword] = false;
      }
    }
 
  return $infos;
}
 
?>

Save the file, and you should be able to activate it in your Yourls admin page under Plugins.

Once Activated this will automatically add an entry for and redirect from short.url/### to the zencart product page (even if the product doesnt exist).

The only problem with this is that if yourls creates a random short url that you later make into a product, it will conflict. (Like if short.url/1001 points to something specific, but a year later you create a product that has ID:1001)

So add into the same plugin.php file another filter to keep it from creating "random" tags that could later become a product.

yourls_add_filter('random_keyword', 'no_numbers');
 
function no_numbers($keyword)
{
 
  while(preg_match('/[a-fA-F]/', $keyword) == 0 ))
    {
      $id = yourls_get_next_decimal();
      $keyword = yourls_int2string( $id );
      @yourls_update_next_decimal($id);
    }
  return $keyword;
}
/home/ladyada/public_html/wiki/data/attic/tutorials/library/software/shorturl.html.1310254556.txt.gz · Last modified: 2016/01/28 18:05 (external edit)