Hacking your own link shortener

TL:DR – If you run your own website, it’s relatively easy to set redirects so that you can share a shortened URL that takes someone to a resource with a much longer address.


Many people are familiar with the various link shorteners that have been with us for well over twenty years now. I think tinyurl was the first one I used, but there are many more like ow.ly, bit.ly, goo.gl, t.co etc. Some of them are hard-coded into websites and apps so that any link (URL, uniform resource locator) you post gets shortened to save resources and allow the app to track.

To hack your own link shortener, you need your own domain name. That can be your website domain, of course, but you could find a tiny little domain like those above and pay to register that and then use it to do the conversions. I am sure lots of people do that and lots of people have code running on their server or whatever to redirect anyone who has the shortened link to the proper page.

Personally, I haven’t bothered with an alternative, so my shortened links are basically of the form “sciencebase.com/someword”.* For example, I have set up the following URL on my domain sciencebase.com/music to redirect to a detailed blog post about my music activities which automatically redirects to the full URL for the page https://www.sciencebase.com/science-blog/sciencebase-to-sciencebass.html which as you can see is far more unwieldy than sciencebase.com/music

I use the so-called .htaccess file on my server to do the redirect which  involves editing the file and adding a line. If your website is on a server that doesn’t use .htaccess there are usually configuration files and server scripts that let you do redirects, or WordPress or Drupal plugins and such, check with your webhost.

redirect 301 /music https://www.sciencebase.com/science-blog/sciencebase-to-sciencebass.html

Of course, the neat thing about a redirect is that it doesn’t have to redirect to something on the same domain, it can take visitors elsewhere, so for example, I have my social media links all setup as simple sciencebase.com links but they redirect almost instantaneously to the actual page. For example sciencebase.com/mastodon takes you straight to my mastodon page https://mastodon.social/@sciencebase.

Indeed, if I’m on some social media platform, then you should be able to find me by using a URL of the format sciencebase.com/social and substituting the word social for your platform of choice, it works for with twitter, spoutible, facebook, linkedin, instagram, bandcamp, soundcloud, youtube, pixelfed, and hopefully any of the other common sites and apps. I’m sure there are a few I’m not using and haven’t linked. There are various other redirects I’ve set up so that I could share the short link sciencebase.com/photos or sciencebase.com/book or similar and take people to a particular page with a much longer URL.

One extra advantage to this method of link shortening is that I can change the destination, so the “/music” link could take people straight to my BandCamp page or the “/book” link could take them to the info page for my new book rather than my old one.

Anyway, that’s how I hack my own link shortener to direct people to particular resources without having to share a long URL or use an external app to do the shortening for me.

*Some visitors tried to access the link where I’d used someword, it didn’t go anywhere, so I set a redirect for it to send people to the blog homepage.