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.
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
- Download the latest YOURLS source from git:
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
- Create a configuration file for Apache:
sudo nano /etc/apache2/sites-available/yourls.conf
- Paste the directives found here into the file.
- Enable the site in Apache:
sudo a2ensite yourls.conf
sudo service apache2 reload
- Create YOURLS database:
mysql -uroot -p
create database yourls;
grant all privileges on yourls.* TO "yourls_db_user"@"localhost" identified by "password-yacho";
Remember to change the username and password accordingly
- Copy and configure 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.
- Enable the Rewrite module:
sudo a2enmod rewrite
sudo nano /var/www/yourls/.htaccess
- Paste the code I have put here into your .htaccess file
- You are all set now visit: yourshorturl.co.zw/admin and complete the set up.
- 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