I am tired of TinyURL getting all of the domain “brand share” from my Blogs tweets. So I wanted to come up with my own solution to create short urls using my blogs domain: BrandonsHead.com.

Yes, my domain is longer than “TinyUrl”, and I assume that yours is too – however, those 5 or so extra characters are well worth the brand awareness (especially if you are cross posting to other social media outlets). And thanks to Mod Rewrite and WordPress, pulling this off is relatively simple.

By adding a rewrite rule to WordPress‘ .htaccess file (in the root of your WordPress installation) – you add 1 line of code, and poof you can have a Short URL like http://brandonshead.com/go446

Here is the code that I added to my .htaccess to achieve this:

RewriteRule ^go(.*)? /index.php?p=$1 [R=301,L]

This code is simply saying, any time a user goes to http://mydomain/go123 take the 123 and treat it like a blog post ID, then the rest of WordPress’ rewrite rules will take over.

Here is what my WordPress’ .htaccess file looks like:


# BEGIN WordPress
RewriteEngine On
RewriteBase /
#Short URL Processing
RewriteRule ^go(.*)? /index.php?p=$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

Also, we want to make it easy for us and our users to copy and paste this new short url. So I went in and added the following code to my Single.php file (that is part of the theme you are using).


<div class="shorturl">
<label>Short URL</label>
<input type="text" value="http://YOURDOMAIN.com/go<? the_ID(); ?>" onclick="this.select()" />
</div>

Disclaimer: This might or might not make your WordPress installation completely blowup, this is only for the brave of heart. I repeat: This might make your WordPress installation come alive and eat your entire city – so please proceed with caution.