Create your own tr.im
Update: an easy WordPress-based shortening solution here.
URL Shorteners can be really useful. Before twitter came along, the main reason for using a site like http://tinyurl.com was to be able to take a long url that wrapped in an email or newsgroup post and shorten it to allow for a better, cleaner view. Sometimes the wrapped URL broke, making it unclickable, too.
With twitter, short urls became even more important. Even those with one or two extra characters (bit.ly vs tr.im or tr.im vs tinyurl.com) lost out in the fray: 140 characters means 2 extra characters is about 1.5% of your message.
The problem is: tr.im went out of business, at least temporarily. With it, went all the shortened URLs. If your web site relied on those URLs, you were out o' luck. Turns out, though, a URL shortener is very, very easy to implement. This tutorial is based on http://wjmp.net/ where my sandbox URL shortener is.
First, the database. I built this in mysql, so change what you need if you aren't using mysql.
--
-- Table structure for table `hits`
--
CREATE TABLE IF NOT EXISTS `hits` (
`id` bigint(20) NOT NULL auto_increment,
`time_stamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
`url_id` bigint(20) NOT NULL,
`ip` char(15) NOT NULL,
PRIMARY KEY (`id`),
KEY `url_id` (`url_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=353 ;
-- --------------------------------------------------------
--
-- Table structure for table `urls`
--
CREATE TABLE IF NOT EXISTS `urls` (
`id` bigint(20) NOT NULL auto_increment,
`url2go2` varchar(255) NOT NULL,
`date_created` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=95 ;
Okay, if that's set up, you need to put up a domain. The shorter the better, obviously, but any will work to test.
You'll need to configure apache, too (or whatever web server you do use). You can use mod_rewrite, which is what most people use, but in a pinch you can redirect 404 File Not Found errors to index.php and parse the redirected path. I'm going to give you mod_rewrite instructions.
Add this to your apache configuration. I think a .htaccess should work, too, but I haven't tested it.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
After you restart your server, you'll have 1) the database set up 2) the web server set up. Now, we just need to add the files. These are in PHP, but this is SO simple that it should be easily ported.
<!-- body { text-align: center; margin: auto; } #srtit { font-size: 16px; font-weight: bold; margin-top: 15px; } --> <script src="jquery.js"><!--mce:0--></script> <script type="text/javascript"><!--mce:1--></script> SHRINK RAY! <div id="shrink_ray"> <input id="u" size="36" type="text" /> <input id="srit" type="button" value="shrink ray it!" /></div> WJMP: each is the first letter on the num pad of your phone. Sorry about the '.net' not much I can do. Some of the other url-shorteners are tracking who is clicking them and selling the information to marketers! The Shrink Ray at WJMP doesn't! <pre lang="php"> « copy that || <a href="/">another?</a> EOF; }
Copy & Paste both into the proper file name (index.php & add.php) and drop into your document root.
One more file, I store this one a level above docroot:
{$msg} EOF; }
You'll also need to download http://jquery.com/ and rename it to jquery.js in the root directory. Or, you can edit the functionality to avoid using jquery
For now, I'm storing "hits" but haven't written a stats tracking system, or a login system. But, if you have those needs, go for it. I also think implementing at least APC but maybe even memcached would be great; the most popular urls would be fetched from a memory cache making it that much faster.
You can create a short URL using a link or bookmark button this way (at least in Firefox on OpenSUSE):
- Go to Bookmarks, Organize Bookmarks, click on Bookmark Toolbar
- Right click, choose Add Bookmark
- Name it ("Shorten URL" or whatever you want)
- Location: javascript:location.href='http://yourdomain.com/add.php?u='+escape(location.href)
Enjoy! Now, you and your web site or company can rest easy knowing that you own your own URLs, long and short.
