Build your own URL Shortener in 2 hours or less!

Created with Sketch.

Build your own URL Shortener in 2 hours or less!

Summary: We will build a URL Shortener service using AWS S3 that is simple and cost effective. It is worth the effort if you are paying expensive subscriptions fees to a third-party service for shortening large number of links every month. Did I mention that our solution doesn’t even require database! How about a complete serverless solution? Want to jump to the solution directly? It’s here.

Hi there! We all have used short URLs as they have become quite popular. A Short URL is simply an alias that points to potentially bigger URL. Short URLs are used because they are easier to share and look clean compared to the long hideous URLs.

Now, if your application sends out Email/SMS containing user specific links, you might have used one of the popular URL Shortener services to shorten your links. Integrating these services are pretty straight forward. You sign up, buy a subscription, get an API key and call the API in your application. You pass-in the long URL and get a shortened URL to use. Simple enough! So this leads to an obvious question –

Why should I build my own URL Shortener?

Most of the popular URL shorteners work pretty well and are quite reliable. However, when your application starts to scale and you need to shorten 10K+ new URLs every month, there aren’t many cost effective solutions available. 

For example, if your application sends out tickets, reminders, etc. to your customers via SMS, each SMS will contain a short URL that is unique to that customer. So your application becomes popular you need to be able to create thousands of new short URLs every month and that is when these services start to get quite expensive. So building our own URL shortener starts making sense and becomes more cost effective. Moreover, having your own service brings in a lot of flexibility that your application might require, like expiring links after a certain period.

We encountered this problem in one of our projects. As we didn’t find a suitable solution, we built our own shortener. It has been working flawlessly for more than 3 months now and we are creating about 25K new links/month!

What about link analytics?

There is only one drawback that you should be aware of… 

Most of these services will provide quite detailed (but generic) analytics about your links like whether they were opened or not, how many times were they opened, user’s region, device, browser, etc. If you care about these analytics, you might want to stick to these services, as our service won’t be providing these features.

But as we said before, in our application, the links are specific to a user, opening the link will take the user to a page that is opened in context of the user itself. So we found it better to build the desired analytics for your product in our backend itself as it is much more focused and precise. We weren’t even going to use the analytics provided by these other services.

Let’s get started…

Now that we have covered the background and the problem statement, let’s look at the solution.

URL Shortener working diagram

We will be using Amazon S3 as our redirection engine to implement our solution. As S3 takes care of the redirection itself, we don’t even need a database. Our solution just requires 2 steps

  1. (One-time setup) Bucket setup in S3 with optional configuration to expire the URLs
  2. Call the S3 APIs from your application to create short links

S3 Bucket setup

First we need to create a new bucket in S3 with static website hosting enabled. Please follow the steps here to create your bucket in S3. Please note that the name of the bucket must match the domain name you want to use in your short links.

 After you complete the setup, your settings should look something like this:

Image Credit: https://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html

It is recommended to enter some redirection rules for broken links in the highlighted section above. If someone lands on a short URL that does not exist or has expired, they should be taken to a relevant page depending on your application. Learn about redirection rules here. Below is a sample you can quickly modify for your application.

https://gist.github.com/rohitgupta27/c0e47b1cc1d95b78e60a6f8a67bd3e99

Optional: If your short links don’t need to be valid forever, it is recommended to setup an expiration policy for your bucket. When you set an expiration policy, for example 30 days, any objects older than 30 days will get deleted automatically. In our case objects map to links. So this will ensure that old links get cleaned up automatically. Please follow instructions here to setup bucket expiration policy.

Creating links from your application

Depending upon your backend, all you need now is call the AWS S3 APIs to create those short links. We use Laravel for our backend and this can be accomplished using the following code written in PHP.

https://gist.github.com/rohitgupta27/712f674074d678f11240e885d01810d3

That is all that’s needed. Your application can call the above piece of code to get a shortened URL and then use the link as needed. When the user tries to open the shortened URL, it will go to S3, which will redirect the browser to the long URL.

That is all that’s needed! Here is how the code works:

  • You pass in a long URL that you want shortened to the above function
  • The code will generate a random unique short 6 character alphanumeric filename
  • It will create an empty object in S3 bucket’s root folder with that short name
  • It will set a metadata on the file which basically tells S3 that when this file is requested, redirect the request to this other URL

Your application can call the above code to get a shortened URL and then send it to your users. When the user tries to open the shortened URL, it will go to S3, which will redirect the browser to the long URL.

Looking for a serverless solution? We already had an application backend, so we just called the APIs from our server to create these short links. But this can easily be converted to a serveless solution by simply implementing a similar function in AWS Lambda.

Short domain name

You might have noticed that S3 URLs are actually pretty big so a file with the name like “xy9kK1” placed in the root folder of your bucket will have a URL like “https://my-bucket-name.s3.aws-region.amazonaws.com/xy9kK1“. So technically, we haven’t really shortened the URL… yet! This last piece of solution is actually quite simple but crucial.

You are going to have to buy a short domain name that you can use for your shortener service and then forward that domain name to your bucket. Depending on your domain provider, all you need to do is enable domain forwarding to your S3 bucket and you will be all set!

How much will I pay to host this solution?

You will be surprised! Since the objects that are getting created are empty objects, your storage costs will be negligible. You will be charged only for PUT requests when you create objects and GET requests when users try to access your links. It turns out that these are extremely cheap and hence you will pay practically nothing for hosting this solution!


Adrobit Technologies is a custom mobile and web application development company. We build apps for startups and big businesses alike. We help them in every step from idea to launch and beyond, using our expertise and 10+ years of experience in software development. Checkout more details on our home page. Contact us if you need any help implementing this solution.

 

×