URL Shortener

So, it’s been a desire of mine for a while to make up a URL shortener. It seems like such a trivial coding experiment, but a fun one.

The first hurdle, getting something better than base10 for the URLs. That’s fairly easy, in that you can look here. The code he lists isn’t complete, as you need some other stuff. So, go grab the Cassis project code instead.

Note: when you “include_once” that code, you’ll want to do it like:

1

2

3

ob_start();

include_once("./cassis.js");

ob_end_clean();

Next you’ll need to code up something to add URLs to your database. That’s also easy enough. My suggestion would be a simple table that would have “id”, “shortcode”, and “url”. Make id a primary key, and add an index on the “shortcode”. Maybe one on the “url” if your going to be using the system heavily enough to require checking to see if a URL exists already before blindly creating a new short-url.

The last hurdle is to write the decoder. Basically, just have it look for “GET”, and look that up in the DB table, then set the “header(location: )” to be the URL in the for that short code.

Now, normally, you’d have a mod_rewrite rule that looked something like this:

1

2

3

RewriteCond%{REQUEST_FILENAME}!-f

RewriteCond%{REQUEST_FILENAME}!-d

RewriteRule shortener.php?url=$1[L]

Except, Wordpress permalinks use something similar, and the two don’t play nice. Now, it would be cool to pass all URLs on to the decoder, and if it didn’t exist in the DB, you’d pass it on to the wordpress index.php. I haven’t quite figured out how to do that, but I found a pretty decent work around.

1

RewriteRule^z(.*)shortener.php?url=$1[L]

Stick that before the Wordpress rewrite rules. What that will do is, if there’s a URL that starts with “z”, it’ll pass whatever comes after that “z” on to the decoder. Personally, I don’t have any pages, or files on my website that start with “z”, so it’s safe for me to do. Your experience may vary. If the decoder doesn’t find the url in the table, it simply redirects to my main page. I think I’ll change that to a 404 here once I am further along.

I’m going to keep playing with this. And hopefully, make some pretty spiffy use of it, but for now, it’s “basically” working. I’ll try to remember to post back on this topic when I have more to report on it. =)

UPDATE 1: I got 404’s to work for non-existant short codes. So, the key with wordpress is to use something like this

1

2

header("HTTP/1.1 404 Not Found");

include("index.php");

Basically, I was trying all kinds of other things, then realized that index.php for wordpress will look for the header state, and redirect appropriately. So, that’s it. It now throws a 404 (which, without that include, it would throw a 404, but it was just a blank page)… Cheers!