Author
By Luke Johnson
Date
Oct 18, 2018
Reading Time
4 minute read
Quick Summary ~ With some basic PHP, MySQL, and HTML

Table of Contents

    Link shorteners are everywhere these days: those small links with scrambled letters and numbers to allow you to share something without having to post the lengthy, original URL. There are the generic ones, like bit.ly, snip.ly, clk.im, branch.io, and others. And then there are the growing number of "branded links", companies who use a custom domain to share shortened links while still pointing you to their digital identity. Here are a few familiar ones:

    • Google: goo.gl
    • New York Times: nyti.ms
    • Starbucks: sbux.co
    • Pepsi: pep.si

    If you wish to set up branded shortlinks for your own organization, there are many service providers available who handle shortlink-generation for a small monthly fee. But you can set up your own branded shortlink service with very little work, and for no extra money. 

    Here's how:

    1. Get a domain

    There is a plethora of domain options nowadays, with over 500 domain extensions beyond the 'vintage' .com, .net, .org options. Think creatively about the name of your organization. As you can see from the examples above, they weren't strict about spelling. The idea is more about reminding people of your brand whenever they see your links. Head to hover.com, name.com, rebel.ca, or elsewhere to grab a domain. (Once you've got one, point it to wherever your website is hosted so you can set up a place to run your link shortener.)

    2. Create a database table to store the shortlinks

    If your organization has a website with a content management system, you could create a new table in the existing database, or just create a brand new database if you'd rather keep things separate. All you need is a single table to store the shortlinks you will create:

    3. Create a form to input your shortlinks

    With the database in place, you'll need a simple form where you will paste in the link you wish to shorten. In this single file, I've included the form and the script that will process the submitted data and create the shortlink.

    The processing script does the following:

    1. Checks that it is a valid POST request.
    2. Looks up the full URL you pasted into the form to see if that same URL is already stored. If it is found, the lookup code is returned. No sense in creating duplicates! If the URL is not found, then a new shortlink code is generated. 
    3. Before inserting the new code into the database, the code is looked up to see if it already exists. If it already exists, a couple more randomized characters are added to the end to ensure that it is unique. If it doesn't exist, the URL and shortlink code are inserted into the database.
    4. Once processing is done, an input field is generated with the new shortlink ready to be copied and shared.

    In my link shortener, I use a 'randomString' function to generate the small string of letters and numbers. Use any code-generator you like. But in case you don't have one, here is mine:

    4. Give your server instructions to understand your shortlinks

    If you're on an Apache server, you'll need to include an htaccess file to allow your server to located a shortlink code in a URL like  yourdomain.com/s28dkDk . The following htaccess rule will interpret the  s28dkDk part of your URL as the 'code'.

    5. Handle shortlink redirection

    Now, with server instructions in place, this final file retrieves the 'code' from the URL, looks it up in the database, and returns the full URL to the header() function, which instantly redirects a user from the shortlink URL to the intended destination.

    NOTE: Throughout this tutorial, you'll have noticed my custom 'query' and 'query_run' functions. I use these to speed up the work of accessing the database. But any connection method will do. The SQL queries themselves will be the same.

    Happy link branding!

    With a little bit of setup, you can save yourself a pile of money in monthly fees, and still get all the benefits of branded links. If you wanted to drill down into the use of your shortlinks, you could add in an analytics script to keep track of how many hits your shortlinks receive. (If you do that, you'll want to swap out the header() function for an html 'meta' refresh redirect instead, since the DOM will need to load in order for your analytics script to fire.)

    If you get in a bind and want some help, shoot me a note at [email protected].

    Web advice in your inbox