Build Your Own URL Shortener

Kevin FOO
2 min readJan 13, 2025

--

I have domain kevn.foo and I’m going to use it to build my own URL shortener for redirection in the simplest way possible without extra bells and whistles.

I’m using Cloudflare as my DNS. First configure the DNS to point to the server’s public IP. My server is an EC2 instance in AWS with Apache installed. Read instructions here on how to install Apache.

Create a file /etc/apache/sites-available/kevn.foo.conf and copy these content into the file.

<VirtualHost *:80>
DocumentRoot /srv/www/html
ServerName kevn.foo

Redirect 302 /li https://www.linkedin.com/in/oofnivek

RedirectMatch 302 ^/.* https://oofnivek.medium.com/oofniveks-stories-8d6a0981a7b9
</VirtualHost>

The first redirect 302 will redirect http://kevn.foo/li to LinkedIn. The second redirect will catch all other paths that are not configured and have it redirect to medium.com. Then run these to enable it.

sudo a2ensite kevn.foo

sudo systemctl reload apache2

In security group, remember to allow inbound rules for port 80 and 443. Once allowed, port 80 should work now.

Next install SSL certificate by running the lines below.

sudo apt install -y certbot python3-certbot-apache

sudo certbot --apache

Once installed, the redirection in /etc/apache/sites-enabled/kevin.foo.conf can be removed because it will be using the SSL version of the conf file. Once updated, it should look like this.

<VirtualHost *:80>
DocumentRoot /srv/www/html
ServerName kevn.foo

RewriteEngine on
RewriteCond %{SERVER_NAME} =kevn.foo
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Say I want to add a new redirection to medium.com, it should be added to /etc/apache/sites-enabled/kevn.foo-le-ssl.conf like this.

Save it and reload Apache. https://kevn.foo/m will now redirect to https://oofnivek.medium.com

< Back to all the stories I had written

--

--

Kevin FOO
Kevin FOO

Written by Kevin FOO

A software engineer, a rock climbing, inline skating enthusiast, a husband, a father.

No responses yet