About Tiny Domain Redirector

Ramit
2 min readJul 21, 2023

Bonjour, sitting on a Friday night I thought to myself, hey won’t it be cool if I can point my subdomain contact.ramit.io to my server, setup nginx as a reverse proxy to an app which will then send a redirect response and take you to say ramit.io/contact .

Now, I know there are other ways to do this (some arguably better). However, I have been learning Golang for a few weeks now, so the geek in me wanted to build something.

Introducing Tiny Domain Redirector — a super simple Golang app that does the above — check if the request is coming from a domain (or subdomain) and send a redirect response to someplace else.

Once the code was done, I built the binary for Linux x86 and deployed it to my server; added a reverse proxy in nginx and pointed my subdomain to the server. Ran my trusted curl (curl -skIXGET contact.ramit.io -L) command and voila, it works!

Repository URL: https://github.com/ramit-mitra/tiny_domain_redirector

The application uses the http package in Go to create an HTTP server that receives HTTP requests and redirects the client to the specified destination URL.

Overall, IMO the code is well-written and well-documented. It is (probably) a good example of how to write a simple Golang web application.

Here are some additional notes about the code:

  • The constant redirectFrom specifies the domain or subdomain that should be redirected.
  • The constant redirectTo specifies the destination URL that the client should be redirected to.
  • The function main() creates a new HTTP server and listens for HTTP requests on the specified port.
  • The function http.HandleFunc() registers a handler for the / path. The handler redirects requests to the destination URL if the request comes from the selected domain or subdomain.
  • The function http.ListenAndServe() starts the HTTP server.

I hope you find this interesting. Comments and suggestions are welcome.

In future, I want to update the code to maintain a mapping for multiple from and to values. This will allow me to point multiple subdomains to this same app instance and redirect them accordingly.

Je vous remercie

--

--

Ramit

Data Science & deep learning entusiast. Polyglot developer. Introvert. Minimalist.