NERD! Homepage

Creating my own small URL generator

UPDATE: check out the extended version of this script: fitting URL.

Since I started twittering a few days ago, and Twitter allows just a few letters for each tweet, the need arose for a small URL generator, a service that replaces a long URL (like http://vasilis.nl/fotos/digitaal/kiki/kiki-2009/in-de-hangmat-05-09018.jpg/) with a small one (like http://vasilis.nl/a/6). I tried two services (tinyurl.com and tr.im) but both didn’t work as I wanted them to work, which is strange because I don’t want much. Fortunately I’m a nerd so I just wrote my own short URL service. I’ll explain what I did so you don’t have to figure out how to write your own. The other reason I write this blogpost is that I hope somebody will explain me if things can be done easier or more secure: I’m just googling for solutions, I’m no programmer. And I really hope that somebody with a math knob can help me count with the base 64 system I choose to use.

Here’s the code for people who rather read code than words.

Put the htacces file and a php file in a directory on your server, make sure that the htaccess file is writable (you probably don’t want it too writable though) and call the php file like this: http://example.com/a/tinyurl.php?tinyurl=http://www.verylongurl.com/

What does this code do? It reads the content of the .htaccess file, reads a commented number inside it, adds 1 to that number, assigns a unique letter/number combination to the URL you want to shorten, writes a redirect rule in htaccess-language and adds that to the original htaccess file. It gives back the new URL in a textfield with all text selected and focus on it. That’s all I want.

First check if a URL is given

<?php
$redirecturl = $_GET['tinyurl'];
if($redirecturl){

These symbols are allowed, 64 in total

$nummers = array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','-','_');

Get the content of the .htacces file

$oldfile = file_get_contents('.htaccess');

By exploding on ‘#’ we get very usefull chunks of data

$aantallinks = explode('#', $oldfile);

$aantallinks[1] is the current number used for the math further on. We need to add 1 for that math

$numlinks = ($aantallinks[1]+1);

Here’s some math abracadabra. Hope somebody can make a less messier function out of it (1

if($numlinks>count($nummers)){
$aantalcijfers = floor($numlinks/count($nummers));
$tweedecijfer = ($numlinks-($aantalcijfers*count($nummers)));
$newurl = $nummers[$aantalcijfers].$nummers[$tweedecijfer];
}
else{
$newurl = $nummers[$aantallinks[1]];
}

The math blob ends here, I can get back to explaining

Here we start writing the new content for the .htacces file
First the coded number we need for the math blob:

$newfile = '#'.$numlinks.'#'."\n";

Next we write back the old redirects

$newfile.= $aantallinks[2]."\n";

Here’s the new redirect

$newfile.= 'Redirect 301 /a/'.$newurl.' '.$redirecturl;

And here we write the new content into the .htacces file

file_put_contents('.htaccess',$newfile);

Create the feedback HTML

echo '<input type="text" value="http://vasilis.nl/a/'.$newurl.'" id="deinput">';

Some script to select the new URL so you only need to copy it.

echo '<script type="text/javascript">document.getElementById("deinput").select();document.getElementById("deinput").focus();</script>';
}

Functional code ends here.
The script ends with a disfunctional else statement which makes sure nothing happens when nu URL is given

else{
}
?>

Now, in an empty .htacces file just write down this:

#0#

(1 My programming and math skills end here. This math blob generates a url with one or two symbols, I don’t understand how to make it add an extra symbol if necessary (for instance after 64×64=4096 URL’s). In other words, I want http://vasilis.nl/a/__ to be followed by http://vasilis.nl/a/000 but I don’t know how to do it.

Mr WordPress | | #

Hi, this is a comment.
To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.

Delicious (Dutch)