URL shortening services, such as Google’s goo.gl, Twitter’s t.co, bit.ly, and many others, provide content authors the ability to point users to a web resource via a link substantially shorter than its canonical form. Especially useful in length-constrained media such as Twitter, link shorteners offer a variety of benefits, from tracking clicks to protecting against malicious content. At Bentley Hoke, we use our own, self-hosted URL shortening service: bent.ly/ENLu, for instance, points to our homepage. (The “.ly” top-level domain is from Libya.)
Benefits of Hosting Your Own Shortening Service
One benefit of a self-hosted, as opposed to externally-hosted, URL shortener is branding: just as “goo.gl” connotes “Google,” we use “bent.ly” to associate the links we present to viewers with “Bentley Hoke.” Our branding recognition isn’t that of Google’s, of course, but we’re working on it.
A second advantage of using our own link shortener is tracking: as with externally-hosted services, clicks on a bent.ly link are recorded. A field in the database record for each URL shortened is incremented each time a user clicks on a link, meaning that we can track how many users followed a shortened URL – enabling us to see how many folks follow a link we tweet, for instance.
Lastly, of course, our shortener allows us to present shorter links. A long URL fits more easily into a tweet and often scans more easily, in an email or print communication, than does a long web address.
Some Assembly Required
(Perhaps best to stop here if PHP/MySQL web development ain’t your bag; forward to your friends among the pocket-protector-sporting set as needed.)
We started with Brian Cray’s shortening code, a small set of files made freely available. The zip download includes a set of SQL commands which create the corresponding MySQL database table. Adding a few additional lines to our site’s .htaccess file
1 2 3 | RewriteBase / RewriteCond %{HTTP_HOST} ^(www\.)?bent.ly [NC] RewriteRule ^(.*)$ http://shorten.bentleyhoke.com/$1 [R=301,NC,L] |
redirects incoming bent.ly requests to shorten.bentleyhoke.com, from which the parameter following the slash after the domain name (per yet another .htaccess file) is decoded to retrieve the relevant database record. If such exists, the incoming request results in a response redirecting to the specified long URL and the “how many redirected” field is incremented.
Thanks to Brian Cray for the free, and well-written, code.