How to run your own URL shortening service

Posted by

Everyone reading this has probably been on Twitter at some point and more than a few are regular users of the service. The observant among you will have noticed the automatic way in which Twitter shortens URLs in the t.co format which is sort of a necessity considering the service’s 140 character limit.

This makes it possible to have functional URLs and use the remaining precious real estate for something else, for example, hashtags.

What is URL Shorting

URL shortening is the practice of shortening URLs (often known colloquially as web addresses). This is often achieved by taking a very long URL for https://support.google.com/webmasters/?hl=en#topic=3309469 and shortening it using a redirect domain for example https://goo.gl/suDzce.

To be fair URL Shortening is not a new concept. It has been around for more than a decade. The web service tinyurl.com has been offering URL shortening services since 2002 long before we had mobile broadband and dial up was the order of the day. They are not the only ones and they are a lot of other URL shortening services out there including bit.ly, goo.gl, ow.ly, wp.me, t.co and Facebook’s fb.me.

advertisement

The benefits of URL shortners

Benefits such as aesthetically pleasing URLs (as shown above URLs can be really “ugly”), results in URLs that are easy to type and typing errors are minimised. They make sharing links easier especially when one is using services like Twitter where the number of characters is limited.

They result in QR codes that use less real estate ( Zimbabweans are not really into QR codes) and make managing URLs easier, for example, all expired URLs can be easily redirected. Shortened URLs are also easier to memorise and they take less space in SMS messages and detailed metrics can be yielded when using URL services.

Why should you use you own URL service?

While you can make use of the publicly URL services there are benefits to hosting your own URL shortener:  you get to use your own short domain name, it might be cheaper this way, you are in full control, you have access to all the metrics and data, it builds the inner geek in you and allows you to learn.

There are several reasons why you should not. There are plenty of advanced services out there and building your own service is akin to reinventing the wheel and if something does break down on your service it might be the stuff of nightmares. Also it might even end up costing more. The delays resulting from use of short URLS are negligible though.

What you will need

You will need the following:

  • A cheap VPS running Ubuntu. There are plenty of these out there just make sure you choose a reputable provider. This is preferable to running this service on a VPS with other production services on it.
  • Make sure your server is running Ubuntu 14.04
  • A running lamp server, you can install it using the tasksel command.
  • The YOURLS software.

Building your URL shortening service

  1. Download the latest YOURLS source from git:
    mkdir /var/www/yourls
    cd /var/www/yourls
    git clone https://github.com/YOURLS/YOURLS/archive/1.7.1.tar.gz
    tar -zxvf 1.7.1.tar.gz
    mv YOURLS-1.7.1/ yourls
  2. Create a configuration file for Apache:
    sudo nano /etc/apache2/sites-available/yourls.conf
  3. Paste the directives found here into the file.
  4. Enable the site in Apache:
    sudo a2ensite yourls.conf
    sudo service apache2 reload
  5. Create YOURLS database:
    mysql -uroot -p
    create database yourls;
    grant all privileges on yourls.* TO "yourls_db_user"@"localhost" identified by "password-yacho";
    flush privileges;
    exit;

    Remember to change the username and password accordingly
  6. Copy and configure YOURLS:
    cd /var/www/yourls
    cp ./user/config-sample.php ./user/config.php
    chmod 0666 ./user/config.php
    sudo nano ./user/config.php

    Modify the file by following the hints within it.
  7. Enable the Rewrite module:
    sudo a2enmod rewrite
    sudo nano /var/www/yourls/.htaccess
  8. Paste the code I have put here into your .htaccess file
  9. You are all set now visit: yourshorturl.co.zw/admin and complete the set up.
  10. Once you have completed setting up the service on the web browser side and verified everything is working return to the command line and run the command:
    sudo chmod 0440 /var/www/yourls/user/config.php

That is all there is to it really. You can create short URLs by login in to your admin account. The interface also allows you to view detailed statistics related to your short URLs.

I should hope you find this tutorial useful especially if you are a control freak like me.

Image credit: makeuseof.com

6 Comments

  1. Anthony Somerset says:

    you don’t even need a VPS for yourls. all you need is a domain and a reasonable hosting provider – lets not over complicate it and focus on the key things

    1. Michael Masanga says:

      i guess thats why the article mentions “This is preferable to running this service on a VPS with other production services on it.”

  2. Rational Ear says:

    Most people in Zim host their websites on shared hosting plans. If your hosting plan supports addon domains or multiple websites, then all you really need is to register a short domain and add it to your hosting account. If you want a url shortener that’s really light weight and simply to install, then try the php script by Brian Cray, https://github.com/briancray/PHP-URL-Shortener. It will even collect stats of how many times a shortened URL has been used.
    URL shortening is really cool and you can shorten over 42 billion unique URLs in 6 or less characters (it can do more than 12,000,000 in only 4 characters).

    Getting a short domain is quite important now and note that all aa-zz.co domains have been taken. However, We still have our own a-z.co.zw domain. Oh sorry, z.co.zw has already been taken!

    1. Garikai Dzoma says:

      I have just checked out PHP-URL-Shortener it is a great alternative and thanks for mentioning the bit about the short domains. Glaring oversight from myself; after all there is not much point in shortening URL using some-really-really-long-domain.co.zw

  3. Tapiwa ✔ says:

    Using URL shorteners is a bad idea, rolling out your own is worse. Here’s why:
    – links die when the service dies, link rot is a terrible thing. Are you willing to run your service for ever?
    – They slow down the web because of redirects. Click on a link in twitter, t.co redects you to bit.ly, and bit.ly then redirects you to where you really want to go. And you could go deeper, inception-style
    – Short URLs are bad for security: would you rather click on badware.com/virus.exe or h8.it/bUNni? Jokes on you: they are both the same thing

    1. Garikai Dzoma says:

      As you have clearly pointed out URL shortening is not for everyone but the truth is links are always dying one way or another on the web and if it would also go without saying that you should keep the service for as long as you make use of its URLs in much the same way you should keep a post or page up if you have referenced it elsewhere in your site.
      The delays brought about by short links are negligible in the modern day world. They certainly pale in comparison to say the Multiple Get requests most pages on the internet are always making among other things.
      As for short links being bad for security malware creators have always been innovative and short links are also a negligible part of their arsenal. Besides blaming short links for cloaking the intended destination is a bit like blaming the messenger.

Leave a Reply

Your email address will not be published.

css.php