I have a custom domain I use for short URLs, tlab.co, which works out to be a lot cheaper than bit.ly for custom domain redirections. While I am in the very early stages of developing the backend system and analytics for the site, I have implemented a very simple Bash script that generates the links and puts them live on the site which I share here.
Customize BASE (the base directory – usually your web root) and the URI for your server as necessary.
#!/bin/bash
BASE="/usr/share/nginx/html"
MYURI="https://tlab.co/"
# Check we have two arguments
if [ "$1" == "" ] || [ "$2" == "" ]; then
# We needed two arguments to proceed
echo "Nope"
exit 1;
fi
# Check if the directory exists
FILE="$BASE/$1"
if [ -d "$FILE" ]; then
echo "$FILE already exists"
while true; do
read -p "Do you wish to overwrite this link? " yn
case $yn in
[Yy]* ) rm -rf $FILE; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
fi
echo "Creating new short url";
echo "$MYURI$1 => $2";
mkdir "$BASE/$1"
cat <<EOT >> "$BASE/$1/index.html"
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0;URL='$2'" />
</head>
<body>
</body>
</html>
EOT