Want to look like a big brand and link to your content using your own short links? It doesn't have to be complicated or expensive. Just takes a little creativity.
Whether you want control of your external links or you want to give the impression that you're a big company with thousands of employees and resources to spare on a trivial branding detail or you're just a geek like me and find this stuff fascinating, custom short links can prove the perfect solution.
However, setting up a domain for short links can be time consuming and/or expensive. You want to focus on the meat and potatoes of your site, not the side of green beans. Enter: the poor man's URL shortener.
- You need a site to forward your short links to, such as a blog. Duh!
- The site's URLs must contain an ID, either in the path or query string. By default, WordPress just appends a query string to the URL for post pages (eg. ?p=123). That works. I chose to have the post ID in the URL path. You can do the same in WP Admin » Settings » Permalinks. Just select Custom Structure and enter
- You need a short domain. Again, duh! If you don't have a short domain yet, a great place to start your search is domai.nr - just start typing in the search box and you will be presented with creative spellings that can form a short domain.
The Magic Forwarding Sauce
Prerequisite #2 is the key to our poor man's URL shortener. We will use the page ID in our shortened links.
All you need to do is drop the following into an .htaccess file at the root of your short domain:
RewriteEngine On RewriteBase RewriteRule ^(\d+)$ http://www.yourdomain.com/blog/$1 [R=301,L] RewriteRule ^.*$ http://www.yourdomain.com/blog/ [R=301,L]
http://www.yourdomain.com/blog/ with your own URL.
The first RewriteRule is the magic sauce. That rule forwards any path that is one or more numbers (\d+) to your site, replacing $1 with that number. For example,
short.co/123 will forward to
http://www.yourdomain.com/blog/123. If you are using the default WordPress setup, you would use the following instead:
RewriteRule ^(\d+)$ http://www.yourdomain.com/blog/?p=$1 [R=301,L]
The last rule just forwards everything else to the root of your site.
R=301 means 301 (permanent) redirect. While you're testing, you could use
R=302 (temporary redirect) instead.
That Was Easy!
That's all there is to it. You now have a short URL service that doesn't use a DB, never needs updated, and automatically creates short links for every new post.
Admittedly, this is a hack, but it serves the purpose, a band-aid until I get around to a full-fledged URL shortening service. Yourls looks promising. Until then, happy shortening!