Run Your Own TinyURL Service With Phurl
URL shortening services are a must if microblogging services like Twitter are to work. In 140 characters, you don't want the URL you're linking to eating up 100 of them. Or worse, it might not even fit. Enter TinyURL, and a bunch of other services that give you a shorter custom URL pointing to the target site. But why not roll your own, using Phurl? Let's do it!
Step 1: Getting Ready for Phurl
- We need to make sure that we can run Phurl, a PHP script that requires PHP 4 or above, MySQL 4 or above, and an Apache web server. It might work with other web servers as well, I don't know but I believe that I saw references to it during my research to this tutorial.
- You'll have to add some code to a crontab file if you want it to be able to prune URLs on a regular basis.
- Also, if you want to use CAPTCHA to avoid spammers, which you probably should, you'll need a PHP image/gd module. You probably have one of these. Also, there are keys for those of you who want to roll reCAPTCHA.
- Download the latest version, 1.5 right now, from the Phurl website.
Step 2: Installation
Setup the domain, folder, or whatever you want to run Phurl in, and upload the content. Then open your web browser and point it to the folder you uploaded Phurl in, and open install.php. In my case, that's http://phurl.tdhedengren.com/install.php.
It's pretty straight forward to fill out. Add the database details needed (I suggest creating a new database for Phurl only, on beforehand since that's less of a security risk than sending a MySQL username and password with creation capabilities over the web) and fill out the domain name. Check rewrite, and decide whether you want to use standard CAPTCHA or reCAPTCHA. The latter will require a visit to recaptcha.net where you'll sign up to get your keys. It is free.
For this example, I'll use traditional CAPTCHA. However, I truly like the idea behind reCAPTCHA, which is built around scanning books and using random CAPTCHA words that way.
Right, so you've filled everything in the way you want it. Submit it. Now you'll see a page with directions to the config.php file, a line for your crontab file (if you decided to prune URL:s from your database), and the content for the .htaccess file. Copy this into new files and name them accordingly. They should all be in the root folder for Phurl, except the crontab which you'll find elsewhere on your server, consult your server install or host.
When you're done, click the link at the bottom of the page, which will take you to your Phurl root URL, a fully working version of your very own URL shortener! In my case, it was http://phurl.tdhedengren.com.
That's it! Give it a go. If you're getting an error telling you that there's no database table with a specific name, you probably forgot to uncheck the top option during the install, which tells the script to just configure and not write anything to the database. Just hit back in your browser, uncheck it, and run again. The actual .htaccess or config.php files won't change, so don't worry about it. A simple mistake, it's silly that it was checked by default.
Step 3: A Bit of Customization
Right, so we're ready to use our cool URL shortener service then? Yes, we are, however, it doesn't look to funky, as you saw above. For instance, the title tag is "example.com" and the copyright is credited the same.
Unfortunately, you can't just login to the admin, which incidentally is located in the /admin folder where you installed Phurl. The admin is only meant to clean out nasty URL:s and such, it doesn't do anything else.
Luckily, there's some simple template files to edit. By customizing header.php, footer.php, and default.css you can style your Phurl install. For instance, I edited out the example.com in the title tag as well as in the copyright tag, and removed the W3C validation buttons that are there by default. I also changed the font in default.css just to make my changes even more obvious.
Unfortunately, the example is located in the index.php file, which would've been nice to leave untouched. It's not too hard to edit it out though, so you should have no issues finding your way. Two minutes later and I've got something at least a little more customized.
Nothing fancy as you can see, but it's easy enough to fit the Phurl script into your current designs.
A word of advice though. If you intend to create a URL shortener service, don't do it with a subdomain to a long domain. After all, this is a bit meaty: http://phurl.tdhedengren.com/bxsb5e
Wait! What I Can I Do With This?
Ah, looking for free business ideas eh? That's OK, you may do that. In fact, there are several uses for URL shorteners, and although some are already out there and available, doing it under your own brand might not be such a bad idea at times.
The most obvious use of running your own URL shortener is availability, and I'm not just talking about the fact that you can have some control over wether the service is online and running or not. That is a benefit for sure, but the more important one would be the risk of a web service closing. Let's say you've been using TinyURL for years, and suddenly it closes down. That's a ton of links that won't work anymore, and the only thing you can do is change them, a tedious chore. What if you mentioned them in a podcast, or printed them in a magazine? Not so easily fixed then, now is it? Running your own URL shortener will make it a lot easier to manage these things.
Let's move on with another pretty obvious one: Branding. Where ever a long URL just won't work, a shorter one with your own URL is way better. The obvious example would be in print. If you're involved in publishing a magazine, for instance, writing out a 190 character long URL in the text will not only look awkward, it will also be a bother for the reader to type off. Sure, you can have all the relevant links on your magazine's homepage, but why force the reader to go there?
Enter your very own URL shortener, which will let you print out shorter URLs using a domain suited for your magazine. So if The Complete NETTUTS Magazine were to run articles in print, and they need URLs printed, they could both have completenettutsmag.com as well as nettutsweb.com, where the latter would run the shortener.
Another, similar, usage would be podcasts. Web savvy people might be OK with hearing you ask them to go to bit.ly/meganowhey, but the not so savvy would perhaps be confused, and maybe also a bit suspicious. But if you use a domain connecting to your podcast, it'll not only fit nicer together, it will also feel a bit more secure for your listeners.
You could also use an URL shortener to hide affiliate links, if you don't want them to show as plainly. It will also be a little bit more easy to manage affiliate links if you have an URL shortener managing them all. Basically, the URL shortener would double as something of an ad system. Naturally, there are a bunch of better solutions for this, but if you just need to hide some links, this might be something to consider.
There are several interesting URL shortening out there that add functionality to the simple concept. For instance...
- SnipURL and ewerl add statistics.
- thinfi adds password protection.
- Ucash.in has some sort of revenue solution.
With some coding skill, you could add a functionality that displays an ad for a few seconds, before doing the actual redirect, for example. Or you could just rely on ads on your URL shortener, getting users to use it thanks to a brilliant (and short) domain name. I'm pretty sure we'll see quite a few cool URL shortener solutions in the future, and a lot of them will probably start with a Phurl install.
- Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.