An alternative bit.ly redirector

For some reason I was in the house by myself one late winter evening when this request from Owen popped into my Twitter feed.

The context is that there are a number of countries and organisations that for some reason block redirectors such as bit.ly. Ethiopia, where Owen lives, was one until very recently [until just after I'd written this solution!]. I like a “how do I do that” type challenge and I had nothing much better to do that evening. So I wrote an experimental bit.ly proxy.

The steps:

Step 1: Set up a hosting account with a dedicated IP address. I needed the dedicated IP address to paste into my hosts file, and this is possible through my host, BlueHost.com

Step 2: In the root of the hosting site, I set up .htaccess to send all 404 errors to index.php (but could be any file if you wanted to set it up on your site). The contents of the .htaccess file:

1
ErrorDocument 404 /index.php

Step 3: Download the bitly PHP class written by Tijs Verkoyen. This is a PHP wrapper on the bit.ly API. Put the class into the root of the site.

Step 4: Register for a free bit.ly user account and apiKey by signing up at http://bit.ly/account/register.

Step 5: Edit index.php to include the following simple PHP code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
/*
 * Test page to lengthen bit.ly urls directly
 * 1.0  11/03/2010  JA  Initial prototype
 *
 */
 
//build the full bit.ly url from calling url
$reqUri = $_SERVER["REQUEST_URI"];
$bitUrl = "http://bit.ly" . $reqUri;
 
// require bitly class
require_once 'bitly.php';
 
// create instance of bit.ly class
$bitly = new Bitly('mybitlyapilogin', 'mybitlyapikey'); //replace the tags with your bit.ly api login and key
 
// get original url from the shortened version
$longUrl = $bitly->expand($bitUrl);
 
//redirect the page
header("Location:$longUrl");
 
?>

This code picks up the REQUEST_URI (the bit after the /) and appends to http://bit.ly, then uses the bitly.php class to return the expanded URL, and simply redirects to the target page. With a bit of editing this could be down to 5 lines of code. The secret is to use the bitly.php class to access the bit.ly api.

Step 6: Modify your hosts file to include the following line:

1
2
 #this should be the full IP address of your site - replace x with numbers!
70.40.xxx.xxx   bit.ly

(in OS X, you edit the hosts file using Terminal and the command sudo nano /private/etc/hosts)

And now all your bit.ly traffic will be redirected through your own site, thus avoiding any IP blockages of bit.ly itself.

As you’ll have noticed, there’s no validation, error checking or anything fancy at this stage. But that’s for another day, if anyone really needs it!

Categories: How To, Tips. Tags: , . Permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">