Matthew Hipkin

Random ramblings of a geek

Simple random short URL creator

Download: http://www.matthewhipkin.co.uk/programs/shorturl.zip

To use this script, you will need:

  • A web server with mod_rewrite enabled
  • PHP & MySQL
  • The ability to create .htaccess files within your webspace
  • A [short] domain name

First thing to do is setup the MySQL database. The following SQL is enough to store the generated URLs.

CREATE TABLE `short_urls` (
`id` int(11) NOT NULL auto_increment,
`shorturl` varchar(10) NOT NULL,
`longurl` varchar(255) NOT NULL,
`deleted` int(1) NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;

Secondly edit database.inc.php to your needs.

<?php
mysql_connect("localhost","YOUR_USERNAME","YOUR_PASSWORD");
mysql_select_db("YOUR_DBNAME");
$siteURL = "http://yoursite.co/"; // Make sure you include the trailing slash
?>

Finally we need to create .htaccess.

RewriteEngine on
# Include any STATIC redirects before this line
RewriteRule ^([A-Za-z0-9-]+)?$ /shorten.php?page=$1

Upload all the files to the root of the short domain's webspace and then call http://yourdomain.co/add.php?u=http://www.somelongdomain.com/somelongpath/somelongfilename.html from your browser, this should display a short randomised URL in your browser window, copy and paste this into your address bar to ensure it is working correctly.

As an anti-spam method I would recommend you rename add.php and shorten.php to something completely different (remembering to update the .htaccess file with the new names).

blog comments powered by Disqus