A month ago, I decided my domain needed its own URL shortening service. We’ve all seen the third party ones like TinyURL, but I wanted my own for several reasons. First, services like those have gotten somewhat of a bad rap because people have used them to send unsuspecting victims to a host of bad sites. Now when people see a link like that, they’re hesitant to click on it and since I don’t use them for such nefarious purposes, I wanted a way to shorten links. Second, when you use third party services, all your links are hosted on someone else’s database. That means someone else may be exposed to links you’re not interested in sharing with the general public or worse… if they turn out the lights all your links are broken. Third, I believe the majority of my usage will be to send people to my own sites. As such, I want my own domain to appear in the URL because well… it’s cool/nerdy to be consistent.
I set out to write my own code but the short of it (no pun intended) is I don’t have the expertise to. I figured that surely someone else would have gone through the same thing and shared their code. A bit of searching and I came across Get Shorty; a free bit of pages and code that will let you do it on your own server. All you need is the ability to install a MySQL database with admin rights, do a bit of tweaking to their code, upload, install and you’re good to go. I’m including the steps I followed below in case you want your own or I wipe mine out on accident and want to reinstall.
- On your host, create a MySQL database with no tables. This is usually done through a control panel type interface where you’ll specify db name and access credentials. Note the name of the db, the location of the db (the interface should show you), the user name and password to access it.
- Download the Get Shorty zip file at the link above and unpack it on your local machine.
- Edit the configuration.php file to include the four pieces of info from the first step.
$db[‘username’] = ‘yourusername‘;
$db[‘password’] = ‘yourpassword‘;
$db[‘database’] = ‘databasename‘;
- Upload everything to a sub-folder on your host. I chose “tiny” for obvious reasons.
- Load the uploaded version of _install.php in a web browser. (e.g. http://derecola.com/tiny/_install.php)
- Follow the prompts to set up your login and automatically install all the appropriate tables in your db.
- Delete your _install.php so someone else can’t run it
- Enjoy your new service by visiting the folder where you uploaded it!
One of the nice things about this piece of code is it lets you make short URLs in three ways. You have it auto-interpret based on the URL you enter, you can specify any alphanumeric string you want or you have it generate a random number. Since it also has an authenticate piece to it, you don’t have to worry about some random hooligans using your service or introducing bad things to your database/tables.
Now I can log into: http://derecola.com/tiny
…to turn: http://chris.derecola.com/2008/12/friend-recommended-unibroue-brand-so-i.shtml
…into: http://derecola.com/tiny/beer
Since I knew NOTHING about MySQL databases a week ago, I’m just enough of a dork to find that amazingly satisfying.