Zero Effort Link Minification with WordPress

If you have ever linked to my blog, you might have noticed that my URL’s are absolutely monstrous in size. Not only is my long domain name rather long, but I also happen to use the “pretty” style of permalinks which includes full date and abbreviated post title.

There is of course nothing wrong with either of these things. Long domains may not easy to type but they can be memorable. Most people don’t even use the address box in their web browser (which used to drive me nuts until I learned to just accept it). These days it is all about on fuzzy search in Google so as long as someone can manage to type in something close to termanotely incaharet somewhere, they will find my blog.

Similarly, long permalinks are actually useful due to their descriptive nature. I like to be able to glance at a link and not only know what it is about, but also how long ago it might have been posted. I would not want to get rid of an useful feature like that.

That said, my links are just super long. Take this one for example:

http://www.terminally-incoherent.com/blog/2015/01/19/utility-spells-in-video-games/

It is an absolute beast, and I would love to have the option to shorten it when I post it on social media for example. Yes, Twitter does minify links by default but the truncated URL still looks quite ugly in your tweets.

I could use a third party minifier such as bit.ly or goo.gl but that is never a good idea. Not only does it obfuscate the links, but it also sets them up for possible breakage in the future when said 3rd party services shut down which is not uncommon. I have seen quite a few of them appear and go under in just last few years, and being backed by a big company does not seem to help. Google might not be going anywhere anytime soon, but they shut down their services all the time. Personally, I got burned twice with them. First time with Google Notebook (an Evernote precursor), and the second time with Google Reader. URL’s are supposed to be forever so I wouldn’t feel comfortable using any service that is not under my control. Ideally I would want to use my own domain for minification of links.

I noticed that WordPress already does support shortened links in a way. If you have permalinks enabled (and you should), WordPress calls your standard, long and “pretty” links the cannonical URL’s. However it also provides a so called shortlink for each post. You can see it shown in the post header on most themes:

<link rel='canonical' href='http://www.terminally-incoherent.com/blog/2015/01/19/utility-spells-in-video-games/' />
<link rel='shortlink' href='http://www.terminally-incoherent.com/blog/?p=18242' />

The shortlink format is actually the default style of WordPress URL’s you get if you can’t be bothered (or you are unable) to set up permalinks. It acts as a fallback, and allows you to access blog posts by their internal database ID even if the permalinks are not working correctly.

So if I wanted to, I could link to my posts using the shortlink format like this:

http://www.terminally-incoherent.com/blog/?p=18242

If you have a short domain name, this might be enough for you. Mine is still too long, and I absolutely hate parameters in the URL. IMHO, it looks unprofessional. However, knowing that this format exists allows you to shorten and prettify it using Apache redirects. For example, I could put the following line in my .htaccess file:

RedirectMatch ^/(\b\d+\b)/?$ /blog/?p=$1

This matches any URL that contains only numerical digits followed by an optional trailing slash, and seamlessly redirects them to the shortlink style URL format. This rule allows me to use something like:

terminally-incoherent.com/18242

Because of the way WordPress handles shortlinks, users who follow the link above won’t actually see it in the address box when the page loads. Instead WordPress will automatically unfold it to the “pretty” permalink format, which is exactly what I want.

This is a huge improvement. Still to long though.

To compress my addresses even further I purchased a brand domain: tein.co Unfortunately ti.co and in.co were already taken so this is the next best thing I could think of. It’s not pronounceable, but it is short, and visually similar to my long domain.

All that was left was to set the new domain to “web forward” to my actual blog URL and I was all set. I use iwantmyname as my registrar and it took literally 60 seconds to set it up:

Web Forwarding

Web Forwarding

From there it worked pretty seamlessly. If you are feeling lazy and you don’t want to type in my full web address you can now simply use tein.co. Similarly, every post now has a genuine short link (displayed under post title) in the format:

tein.co/18242

Because the entire thing is implemented via 301 redirects, if you post one of these short links on Twitter you will still get that sweet Twitter Card action.

So there you have it. Zero effort link minification using built in WordPress addressing scheme and a single Apache redirect statement.

This entry was posted in sysadmin notes and tagged . Bookmark the permalink.



3 Responses to Zero Effort Link Minification with WordPress

  1. Your super long URLs have been a very minor annoyance for me in the past. :-) Specifically when I link to you from one of my articles. I write everything in Markdown and keep it under 70 columns (Emacs’ default wrap). Your URLs often go beyond 70 characters, and since URLs can’t break, they extend out into the void to the right of my paragraphs! Check out the bottom of this particular article source, for example,

    https://raw.githubusercontent.com/skeeto/skeeto.github.com/master/_pos ts/2012-12-29-An-Emacs-Pastebin.markdown

    However, you’re still certainly not in the wrong. My own URLs only have the date, so when I’m managing a few of them at once, such as when backlinking to my own stuff, I easily get them mixed up. I think I’d prefer the descriptive URL by default, but it’s too late for me to fix that now (though I could cheat with a descriptive fragment). I flirted with a local shortening scheme like yours for awhile, also using an Apache rewrite, but that broke when I switched to static GitHub hosting (no more HTTP redirects). According to my logs, no one was using it anyway, so I didn’t worry about breaking it.

    Reply  |  Quote
  2. Max NETHERLANDS Mozilla Firefox OpenBSD says:

    Now I’m curious, how pissed off were you really about this not-using-the-address-bar thing? Because that blog post kind of reminds me of the Angry Video Game Nerd routine (as in, it’s funny because it’s so silly to get upset about that).

    Reply  |  Quote
  3. Luke Maciak UNITED STATES Google Chrome Linux Terminalist says:

    @ Chris Wellons:

    Yeah, Git hosting is great but limited. Then again, the entire point of static sites is to offload everything to the client side, so using Apache redirects to do some server-side magic kinda flies in the face of that. Git also does not like third party Jekyll plugins, which kinda sucks. :(

    @ Max:

    Well, not that terribly pissed. The post was mostly a shtick for the effect. ;) I did used to sigh a lot more when watching people use their computers back then. I guess I’m more patient now. I still try to teach people power user tricks whenever I can, hoping that repetition will make things stick eventually. :P

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *