Bits and bytes of code
Bytes is my collection of short-form posts, tips, and things I learn as I build software.
Bytes is my collection of short-form posts, tips, and things I learn as I build software.
Netlify has support for creating redirects and rewrites using the
_redirects file syntax. Borrowing from an idea I saw from
Kent C. Dodds and
@caarlos0, I
built a script to make it easy to create short links for my website or
other sites that I can then access via my newly minted go.mskelton.dev
subdomain.
Here is the script in it’s entirety, feel free to peruse from the comments:
#!/bin/bash
args=()
status=""
# Parse the command line args
while [[ $# -gt 0 ]]; do
case "$1" in
--rewrite)
status="200"
shift
;;
*)
args+=("$1")
shift
;;
esac
done
Using the script is as simple as this:
short https://www.google.com googleThis creates the shortlink: go.mskelton.dev/google that redirects to
https://www.google.com. The nice thing with this approach is that Netlify
handles all the redirect traffic so I don’t need that load on my main site,
which helps with reliability of my short links. If I take down my main site
(which I do from time to time 😂), the short links will still work.