Quantcast
Channel: blyon.com » Twitter
Viewing all articles
Browse latest Browse all 6

Build your own URL shortener

$
0
0
There are a lot of URL shorteners out there such as bit.ly, tinyurl.com, is.gd, tiny.cc. The list of services is large. For those of you that don’t know what a URL shortener is, it’s a piece of software that converts very long URLs such as:

http://www.blyon.com/blog/index.php/2010/08/28/build-your-own-url-shortener

Into: http://blyon.com/?efa

This make the URL easy to cut and paste and reduces the characters you use for things such as Twitter.

I’ve been using TinyURL.com for ages. This morning I wondered to myself, “why not just use blyon.com?” After all, the domain is shorter than TinyURL by 3 characters and if it’s just me using my service the hash only needs to be about 3 characters long because I am not storing millions of URLs. Other services use a long hash (7+ characters) because they need more unique ways to identify URLs.

It took all of 30 minutes to write the code, and maybe another 30 to debug it.

Here’s how it works and how to setup your own!

Go to http://www.blyon.com/t to input the URL and get the short URL which uses http://blyon.com/? as the base. There’s a simple PHP script inside /t/ which creates the hash and puts it into a two table MySQL database. I used /t/ just because I already had something at the root of blyon.com.

I added another snippet of code on blyon.com’s index.php page which figures out if someone is trying to lookup a hash or not. If it finds a hash that’s valid, it does a 302 redirect to the right location. The 302 redirect is an easy location director built right into HTTP, so basically you’re telling a browser that goes to the short URL to go somewhere else.

Now, why do this? For starters I was trying to wake up and wanted to have something to do while drinking coffee (the NPR stream was down). However, there may be a lot of SEO reasons to do this. For one, all my links I post are linked back to blyon.com, which means if something I tweet goes around the block a few times, it will all point back at blyon.com. Taking this one step further might mean that I should also allow for special keywords to be created, thus helping google learn indexicals that I want valued for my web site. The third great reason to do this (besides keeping oneself busy while drinking coffee) is to maintain your own links. If TinyURL goes down someday, everything you have ever linked to will stop working as well. However, who’s to say your (or my) admin skills are better than theirs.

Anyway, here’s the code!

The entire package as a tar: redir.tar.gz

To create the TinyURL you need this PHP code (my /t/ index page): index.php

The database include code: dbconfig.php

To figure out if a request is for your main index page or a shortened URL: mainindex.php

The MySQL database you’ll need: redir.mysql

PS: Thanks to Nathan Hickson for helping me debug this.


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images