URL (web address) shortener
I made a URL shortener you can find at gomj.uk.
A menswear business I work with Montague Jeffery was previously using a URL shortener goo.gl to provide links to pages on their website. In other words they were turning a long web address into a short one. For example, gomj.uk/Kf18 looks better than http://www.montaguejeffery.co.uk/panama-hats-from-olney-headwear-3701/ and also if the link is too long then it would either break the fixed width of the newsletter design or potentially be spread out over multiple lines. So for a while goo.gl was working well.
However, this service Google announced was closing down earlier this year. This in addition to legitimate links to the MJ (Montague Jeffery) website being false flagged as spam meant that I wanted to take control of our short links by making my own one, which would ensure the short links they create would still work for many years to come. Creating and maintaining my own instead of using a third party service would also present a lot more control over things such as performance, marketing campaign insights and more.
Like most prudent developers do I started researching existing self hosted URL shortener projects that are freely available, actively maintained and mature enough to ensure the software was good while also being easy to install and maintain. However I couldn’t find anything suitable for my needs that wouldn’t provide a lot of bloat and end up on life support in the near future.
So I got to work on making my own one. This was my process for building it.
My requirements for the URL shortener
There are some things I needed from a URL shortener which I had to keep in mind while thinking about how it would work. These include:
- almost limitless possible links I can shorten
- the shortest possible short URL, within reason
- ensuring every short URL was unique, so there would be no duplicates of existing ones accidentally changed
Achieving almost limitless possible links
Per a week MJ need 2-5 short links. If I do some math then assuming 5 links a week remains the average they are likely to need around 260 links a year, so over the next hundred years they will use less than 26,000 short URL’s.
To ensure I can have at least 26,000 short links and scope to publish a lot more in the future, I decided on 4 characters for the URL path (the bit after “gomj.uk/”).
With an alphanumeric URL path (including both uppercase and lower case characters) then this provides 218,340,105,584,896 possible short URL’s. As you can see this is well above the 26,000 requirement.
Ensuring every short URL is unique
Instead of giving every URL path a number like “1”, “2”, “3”, etc, it would provide me with more options if I use an alphanumerical value because after 9999 I would have to make the URL path longer.
The problem is every row in the database has to have a numbered identifier (not strictly speaking but for this project I do). So every ID I convert into a base 64 encoded value, meaning whatever the binary value is of the ID (for example 2 in binary is 0010), this would be converted into a different type of computer language which uses both letters and numbers. So instead of 10 for example which requires two characters, it could instead be represented as “a” which requires just one character. In other words it works a bit like algebra. That’s how I can generate a unique URL path based on the ID of the record.
Once the ID is generated my system checks to make sure there’s no duplicates. If it fails for some reason (in the case of something like a duplicate) it will just take an existing one from top of the list of URL paths already generated. This helps make the service work faster if there’s no additional checks or processes needed to try and create another. Instead it just gives you what you want (a short URL).
Every now and then I might have to check there’s still a surplus of short URL’s that can be provided ready to claim, although it’s highly unlikely to be an issue and if so then I will create a cron job which will automatically generate new ones regularly as needed.
So far so good. During the short time I’ve been using this it is still going strong and provides a very easy way of shortening my web links.
You are welcome to check it and try it yourself here if you would like to gomj.uk