How to create your own URL shortener?

Suhas More
4 min readDec 2, 2020

A URL shortener is a tool which takes a long URL and turns it into a short URL, In this blog post, we’ll see how to create our own URL shortener.

perfect balance

First, we’ll understand why do we need a URL shortener and then we’ll see how to create one.

Why?

Let’s just see some statistics about the internet,
According to hostingtribunal, 70 million blogs get published each month,
Google receives 40,000 queries per second,
There are 4.18 billion mobile Internet users,
There are 1.75 billion websites.

Hush!!, That is huge

Since billions of webpages and web content created daily, creators need a way to attract traffic to their own content, and social media is one of the best ways to reach out to a large group of people.

Along with social media, our content must rank high in search engines like google, bing, etc to attract another set of users to our content and to rank our content high in search engine algorithms along with SEO, human-readable URLs play a huge role.

Google search results

As you can see in the above search result,
The top 3 results have clearly readable URLs, and the URL contains the search term in it.

This clearly shows us that a readable URL is directly related to the search rank of the content. Although there are a lot of factors in play here we’ll ignore them.

Why shorten them?

As I already mentioned above, social media plays a great role in attracting traffic to the website and post on social media has some restrictions or some unwritten rules to attract more traffic to social media post like,
Twitter restricts the content to 280 characters per post, this rule alone restricts a user from posting long content in a single post. So we must not waste space in URL

Usage of short URL

Above Twitter post uses most of the characters to describe blog post rather than to a long URL.

It uses a short URL in a Twitter post, and below is the actual URL where the short URL redirects the user

Actual Human Redable URL

Apart from the above usage of short URL,

They are easy to manage,
They are easy to share,
They can be used for analytics

That all sums up why we should use short URLs.

HOW?

There are plenty of online services which provides a URL shortening feature like,
1. bitly
2. Rebrandly
3. Shorby

You can start using them right away,

But if you want to create your own URL shortener, stick around for a while

How to create a custom URL shortener?

We are going to use, Netlify and GitHub to create our own URL shortener,

First, you have to create a brand new repository for URL shortener,

create new repository

add a new file called _redirects

which looks like this

_redirects file

Add a few redirection instructions in _redirects file as below

/portfolio is a unique identifier for URL https://suhas010.com
/*
is a placeholder for all other undefined identifiers

That's all on GitHub,

Now go to Netlify and create an account if you don’t have one,
and deploy repository which we just created, and under the Site settings provide a custom short site name to an application

Site settings

You can also provide a custom domain there if you have one

That's all,

https://suhas010.netlify.com/portfolio will redirect you to https://suhas010.com

https://suhas010.netlify.com/usac will redirect you to a blog https://suhas010.medium.com/understanding-scope-and-context-in-javascript-suhas-more-8f765e3867ba

and so on

In this case, our domain name is already long suhas010.netlify.com
it will make more sense if we buy a short domain-like srm.io which will make our short URLs like srm.io/usac

Here is a repository which I have created for my URL shortener and my Netlify app is running here

If you liked it, feel free to share and like,
If you have any query feel free to reach out to me on Twitter

--

--