The Incredible Shortlink Technology Page of Doom (TM)
To set up your own shortlink thingy there are a few pre-requisites that you will need:
- A database. I'm using mysql, and that's what this documentation will assume. There's no reason you can't use postgres, or oracle, or any other database supported by the perl DBI. UPDATE: as of 0.2 postgres is supported out the box.
- A webserver that supports perl. It should work on any version of perl upward of 5.5, and might work on 5.0, but I haven't tested it.
- The CGI module. This is included in any recent perl, so you probably don't need to worry about it.
- The DBI module, and the DBD::mysql (or DBD::Pg if you're using postgres) module. Any decent webhost that supports mysql should include these. If yours doesn't, I highly recommend notnet.co.uk
- A basic understanding of how cgi scripts, mysql, and unix works.* Coffee.
Grab the source code, untar it, and cd into the newly created shortlink-0.2 directory. You'll need to create a database, and a user with permissions to insert and select on the table we'll create. To do this with mysql, enter the following commands:
mysql -u root -p create database shortlink; grant select, insert on shortlink.links to 'user'@'localhost' identified by 'password'; flush privileges; quit;
This assumes you have root permissions on the database in question - if you don't, you will need to check with your hosting providers documentation on what username/password/database to use.
Next, you need to create a table for the links to be stored in. To do this, type the following:
mysql -u root -p shortlink < shortlink.mysql.sql
Use the shortlink.pg.sql file if you're using postgres.
This will create a table called 'links' in the shortlink database, using the SQL create table command that's in the shortlink.sql file.
Now, load up the shortlink.cgi file in your favourite editor and change the database connection details to whatever you used when you created the database, and change the base shorturl to suit wherever you're installing it.
Finally, you'll need to make the script executable. To do this, type:
chmod 755 shortlink.cgi
Now you should be able to open a web browser, and point it at your shortlink script. If all has gone well, then you will get a text box to enter your long URLs in.
Yup. If you want to make your shortlinks even shorter, you can use the script as the index file in a directory, as I've done with http://spod.cx/s. It's cheating a bit, but by creating a directory with a short name, and renaming the shortlink.cgi to index.cgi then you can ignore the filename of the script, assuming your web host supports index.cgi as an index page.
Those of you who happen to run apache can use the ever funky mod_rewrite to make creating shortlinks from your own website even simpler. Make sure you have mod_rewrite enabled, then add the following lines to your apache config file:
RewriteEngine On RewriteRule /(.*),shortlink http://spod.cx/s?url=http://spod.cx/$1 [R]
The first domain in the rewrite applies to the script location - in this case http://spod.cx/s. The second is the domain for which the shortlink shortcut applies. Assuming a setup like this, you could go to http://spod.cx/a/long/page,shortlink and it will auto generate a shortlink for you.