Run Your Own TinyURL Service With Phurl

Run Your Own TinyURL Service With Phurl

Jan 13th, 2009 in Other by Thord Hedengren

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!

PG

Author: Thord Hedengren

This is a NETTUTS contributor who has published 4 tutorial(s) so far here. Their bio is coming soon!

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.

Availability

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.

Branding

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?

Multiple URLs

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.

Podcasts

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.

Additional Functionality

There are several interesting URL shortening out there that add functionality to the simple concept. For instance...

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.

Happy shortening!

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Related Posts

Check out some more great tutorials and articles that you might like

Enjoy this Post?

Your vote will help us grow this site and provide even more awesomeness

Plus Members

Source Files, Bonus Tutorials and
More for $9 a month for all TUTS+
sites in one subscription.

Join Now

User Comments

( ADD YOURS )
  1. PG

    Kevin Martin January 13th

    Good one, Thord ;)

    ( Reply )
  2. PG

    Tommy M. January 13th

    Very clever! I think it’s best used as a foundation on how to build a better URL shortener and perhaps learn from the way it works.

    I always said that when they have an install/generator for it, it’s already overused.

    ( Reply )
  3. PG

    Brenelz January 13th

    Nice. Clever idea that I wouldn’t have thought of.

    ( Reply )
  4. PG

    Jhay January 13th

    This is great! Thanks!!

    ( Reply )
  5. PG

    NetOperator Wibby January 13th

    Hey, this is pretty sweet! Thanks a lot for this.

    ( Reply )
  6. PG

    Nouman Saleem January 13th

    doesn’t seem to work for me: http://minify.me/ try inserting a url and then having using the one it creates.

    must be something I did, I will keep trying

    ( Reply )
  7. PG

    insic January 13th

    thanks for the tips

    ( Reply )
  8. PG

    Max January 13th

    Very useful indeed – thanks for sharing.

    ( Reply )
  9. PG

    halli January 14th

  10. PG

    Zy January 14th

    Well.. personally I don’t think, that this will find a good use in nowadays world, where are full of CMS with built-in URL shortening features. If you need more advanced customization, simply use Apache (or other web server) mod_rewrite module. You probably will be amazed of its features :D

    ( Reply )
  11. PG

    xQlusive January 14th

    mod_rewrite ;-)

    ( Reply )
  12. PG

    Web Hosting Reviews January 14th

    Thanks for letting us know about this free script :)

    ( Reply )
  13. PG

    JumboURL January 14th

    Aren’t you tired of all that simple and small URLs?
    Try a URL Enlarger!
    http://jumbourl.com
    ;-)

    ( Reply )
  14. PG

    Agahmemnon January 14th

    how to add own short name to link? i dont want to use random signs

    ( Reply )
  15. PG

    Mark Bowen January 14th

    Nice link to the script so thanks for that.

    A bit of a shame that the demo you show in your screenshots has less letters when it started than when it finished. You actually ended up creating a longURL instead ;-)

    Never the less thanks for the link. :-)

    Best wishes,

    Mark

    ( Reply )
  16. PG

    unimpressed January 14th

    Sorry to be a scrooge on this, but it’s not really a tutorial – or even a useful article. Why not submit a tutorial on how the concept works properly, and how to develop your own solution from the ground up rather than “here’s a script that does x, here’s how to install it”.

    It’s really not that difficult to implement your own version of this sort of site – and readers would benefit so much more as they could then implement the same principles in other projects (such as blogs/frameworks/cms).

    Come on, quality not quantity please!

    ( Reply )
  17. PG

    Wayne January 14th

    Very cool. Thanks for the tip.

    ( Reply )
  18. PG

    Jauhari January 14th

    I will use it ;)

    ( Reply )
  19. PG

    Jash Sayani January 14th

    This is one of the best articles!! Amazing work Thord !!!
    Thank you!

    ( Reply )
  20. PG

    Timothy January 14th

    Interesting. Thanks.

    ( Reply )
  21. PG

    r3l4x January 14th

    Great tip! Thank you!

    ( Reply )
  22. PG

    mememe January 14th

    nice and very timely as something like this was on our ‘todo’ list!

    ( Reply )
  23. PG

    Thanasis January 14th

    Thx man :-)

    ( Reply )
  24. PG

    Moksha Solutions January 14th

    really cool one, will try and make it in asp.net

    ( Reply )
  25. PG

    Lee Rickler January 14th

    can’t get shorter than http://z6.co.uk

    :)

    ( Reply )
  26. PG

    Nouman Saleem January 14th

    fixed my problem I said I had above, thanks for this. Great for personal use :)

    http://minify.me/

    ( Reply )
  27. PG

    AlpiXXX January 15th

    “has a length of 20 characters and resulted in the following shortened URL which has a length of 46 characters” :D

    thanks, it seems really cool.

    ( Reply )
  28. PG

    iEthan January 15th

    I’m not really fond of services like this. I mean, will you remember iargreenteam.site50.net or tinyurl.com/7648jfk?

    ( Reply )
  29. PG

    Bauer January 15th

    This script needs more features. I like how http://phaze.me lets you pick the end part of the URL, and also has password protection and whatnot.

    ( Reply )
  30. PG

    Sherwin Techico January 15th

    @Lee Rickler: There’s ones such as http://is.gd =)

    ( Reply )
  31. PG

    Omair Rais January 16th

    Nice.

    Thanks
    Omair Rais
    http://www.omairarts.com

    ( Reply )
  32. PG

    Gio January 16th

    I had a personal project very similar to this.

    http://shurtn.com/

    ( Reply )
  33. PG

    Lee Rickler January 16th

    Not any more:

    The following URL http://is.gd has been reduced from 12 to 22 characters:

    http://z6.co.uk/ys5ftw

    :)

    ( Reply )
  34. PG

    Lee Rickler January 16th

    The listings (in admin) aren’t working on mine.
    They are available by clicking each number/ letter but not as one list.
    Anyone know how to fix this?

    Also, custom tags/ endings would be sweet.

    ( Reply )
  35. PG

    Geoff January 16th

    This is very cool, but I can’t get it to work. When I do the install, I don’t get anything for the .htaccess file.

    Any ideas?

    ( Reply )
  36. PG

    Fotomanijak January 17th

    I had it installed it on the nice short domain http://okrati.me
    It’s in serbian meaning “shorten me” – and it’s on the montenegro domain .me – nice is that it means the same in montenegrian lang also ;)

    Now the hard part customization and branding…

    ( Reply )
  37. PG

    Nouman Saleem January 17th

    @Fotomanijak I got lucky with minify.me :)

    ( Reply )
  38. PG

    Gio January 17th

    The only thing that I don’t like about this application (phurl) is when you tried to enter an existing url it generates a new short url instead of using the one that is already in the server. This is crucial when you are trying to use statistics to keep track of the short url.

    ( Reply )
  39. PG

    Nouman Saleem January 17th

    @Gio that’s the whole reason. If your keeping stats you want to know how many people clicked a single URL not clicked through to a website. Especially if your creating a user system such as I am, you want people to have their own links.

    ( Reply )
  40. PG

    Krótke Linki January 18th

    Thanks for this one. If you want to see how can it look check the link.
    Domain name is … silly if you can understand polish language.
    However – it works and it’s really simple.

    ( Reply )
  41. PG

    Mike January 18th

    Check out http://goph3r.com I like it better than tinyurl.

    ( Reply )
  42. PG

    Tolana January 19th

    @iEthan, to answer your question, “will you remember iargreenteam.site50.net or tinyurl.com/7648jfk?”

    Neither!

    ( Reply )
  43. PG

    Jonas January 20th

    Hmm, I have seen better. Don’t work
    —-
    RL has been shortened!

    The following URL:

    http://www.designboom.com/weblog/index.php

    has a length of 42 characters and resulted in the following shortened URL which has a length of 46 characters:

    http://www.hido.net/projects/phurl/demo/2gcrev
    —-

    I think the demo should work

    ( Reply )
  44. PG

    Ed Pelesh January 22nd

    Sorry, but I don’t get it.. Isn’t it useless to invent a wheel? It could be really better if you told us how to implement it as a part or module of some other CMS but not as an independent application.

    ( Reply )
  45. PG

    Taylor Satula January 24th

    I don’t think anyone has suggested http://tr.im/ yet, your not gonna get any shorter than that

    ( Reply )
  46. PG

    monaye January 28th

    There is another program you can install to own server to shorten URL http://get-shorty.com/
    I haven’t try both so I can’t comment on them. Let me know if you have a chance to try it.

    ( Reply )
  47. PG

    Bryan P January 29th

    I like the angle of doing it for branding. If we use a subdomain wont that add extra length to a url? Is the only option to by a new domain?

    And wont that end branding?

    ( Reply )
  48. PG

    Dustin February 6th

    Could phurl be used to create sites like http://song.ly and tinysong.com?

    ( Reply )
  49. PG

    Mohamed Saad February 19th

    not that useful but good practice

    but if you think about the code generated randomly
    you’ll find that RANDOMNESS INCLUDE REPETITION
    this will cause a problem when 2 different URLs are stored with the same code
    you can overcome this by using an AUTO_INCREMENT field like the id and add it to the random code

    ( Reply )
  50. PG

    Hidayet Dogan April 21st

    First, thanks for a nice article. Version 2 is on the way. It’ll be released this month. URL generation method will become like TinyURL one (they use base32 encoding) and there will be custom aliases and preview feature at first. Password protection and statistics will be another nice features of version 2 series (first release won’t have them).

    ( Reply )
  51. PG

    Matt May 23rd

    I’d like to recommend urlShort (http://u.mavrev.com/pro). It features full creation and redirection (HTTP 301), as well as an API for creation and lookup of URLs, custom names, with many more features planned.

    ( Reply )
  52. PG

    jake June 16th

    try .tk=)

    ( Reply )
  53. PG

    Boy81 October 22nd

    VW comes up with all kinds of rather complex mechanical ideas like dual clutch and those variable continuous gears. ,

    ( Reply )
  54. PG

    Sharif November 18th

    Nice tutorial.
    Same as http://www.l4u.in
    Check this out :)

    ( Reply )
  55. PG

    Jan December 28th

    I can not find install.php in zip file, please help.

    ( Reply )
  1. Arrow
    Gravatar

    Your Name
    December 28th