Make A Short URL Of Your Wordpress Posts And Pages – Short Tutorial

Wordpress Short URLs
One week ago, one of the many URL shortening service provider, tr.im, announced that they are shutting down. Services like tr.im, bit.ly, shorturl and tinyurl have become important since Twitter came about. Admittedly, to many people, the shutting down of just one provider is nothing to worry about. There are still many others out there.
Still, the closing of tr.im does bring about the question of how long before others shut down too? What will happen to all the short urls already created? Well, simply said, they will no longer work to bring visitors to the desired destination.
The Problem To Those Who Use Short URLs Extensively
Many people, especially Internet Marketers make use of short urls for various reasons. Internet Marketers are the ones who will probably be effected the most with the shutting down of Short URL sites. When the service goes down, so does all the short links.
Do you have a large number of tiny urls generated for you? Have you posted them in various websites to bring traffic to your domain? Guess what.. You will have to generate new tiny urls. You will also have to edit all your postings, if you can, and put in the new short urls you’ve created. Otherwise, you’ll certainly lose all the traffic you’ve enjoyed in the past from the short urls you’ve created.
Make Short URLs Using Wordpress On Your Own Domain
If you use Wordpress as your website or blogging platform, you’re in luck. You can configure Wordpress to generate Short URLs automatically. All that’s needed is a few lines of code which you can copy and paste into your themes and htaccess.
The following short tutorial will configure Wordpress to generate short urls using the post and page id – yourdomain.com/b/post-id for blog posts and yourdomain.com/p/page-id for pages.
Let’s get directly to the codes. First, copy these into your .htaccess file
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^b/([0-9]+)$ /index.php?p=$1 [QSA] RewriteRule ^p/([0-9]+)$ /index.php?page_id=$1 [QSA] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
Next, you will need to edit one your theme file – header.php and insert the following lines
<?php if(is_single()) { ?> <link rel="shortlink" href="<?php bloginfo('home') ?>/b/<?php the_ID(); ?>" /> <?php }?> <?php if(is_page()) { ?> <link rel="shortlink" href="<?php bloginfo('home') ?>/p/<?php the_ID(); ?>" /> <?php }?>
The code above will insert the shortlink meta tag into your webpages. This meta tag will be used by services like Twitter to determine the short url to use fro your web page. Twitter will now use the short url indicated by this meta tag instead of generating a new url for your page.
The place to insert the code above is within your themes <head></head> space. There should be a Wordpress Loop in there. Just insert the Php code above somewhere in between the loop.
That’s all there is to it. After you’ve added the above codes into place, your wordpress installation will generating your short urls for you automatically. You no longer need any third party URL Shortening Services. Your short urls will be available as long as your site is up.
The Shortcomings Of A Wordpress Generated Short URLs
Not everything is fine and dandy with hosting your own Short URLs with Wordpress. As with anything in life, generating your own short url with Wordpress do have its downside. Lets take a look at a few of them which I can think of right now.
- Lack of statistics. If you are of the type that likes to have statistics on your Short Links, you will probably not like this. I don’t think there will be any statistics available for this type of Short URL implementation.
- Cannot insert special code. Most third party URL Shortening Services allows you to insert special codes or notes to the generated short urls. This is not possible with this method. This method does not allow any input from you part. Short URL is generated using post and page id respectively.
- You can only shorten links for posts and pages hosted in the Wordpress environment. If you want to shorten pages external to Wordpress, you can’t with this method.
- Should you somehow have to rebuild your Wordpress installation from an XML export of your current site, you will lose your old shortened links. Wordpress generates new ids for your imported posts and pages.
- Of course, your domain name should be relatively short for this to be effective. If your domain name is like thisisasampleofaverylongdomainname.com, then it’s best to just use the many third party URL Shortening Services.
For Those Who Don’t Deal With Coding Your Own URL Shortener
If meddling with your .htaccess and Wordpress header theme files is not your cup of tea, then you can go the Wordpress plugin way of doing the same thing. Yes, there are quite a few plugins out there on the net for Wordpress that will enable you to shorten your urls.
Among the few which I’ve tested are:
- Short URL Plugin
- Short URL Generator
- Short Post URLS
- Custom URL Shorter
- Short Link
- RevCanonical
- ShortURL
- Get ShortLink
Yes, I’ve installed and tested the plugins above. However, in the end, I decided to go without any of them. Why? Not because the plugins are not good or anything. I just wanted to minimize the number of plugins I have installed on my Wordpress installation. Since the coding is not too difficult, I decided to run without plugins.
Another URL Shortening Alternative
If you want to host your own URL Shortener and require more functions than what Wordpress is capable of providing, then the following are a couple of alternatives you have.
- Install YOURLS on your hosting server. YOURLS is a server based web application. You can basically just install it into your domain hosting account and run it there.
- Build your very own URL Shortening application. I found an interesting basic guide on this on the net. If you feel like jumping into Php codes and SQL queries, head on over to Sean-O’s URL Shortener Tutorial.
URL Shortening Services are here to stay. Even if Twitter currently favors bit.ly to shorten URLs submitted to them, other Shortening Service Sites will probably continue to function as usual. Want to know how important a shortening service is? Well, users of tr.im have managed to make the owners of tr.im to go back on their decision to shut down tr.im. They are now making tr.im a community owned project.
Do you use URL Shortening Services? Have you thought about what will happen to your short links if the provider goes under?
The Short URL for this post is: http://kodeexii.net/b/1103
That’s certainly way shorter than the original url: http://kodeexii.net/current-events-computer-technology/make-short-url-wordpress-short-tutorial.html





Thanks for the article – exactly what i was looking for. I’ve seen people start to shorten their own URLs just for controlling their own destiny. If one of the URL shortening services goes under and nobody continues where they left off, big problem.
Thanks again – Chris
Minneapolis SEO Guy – Chris´s last blog ..Deconstructing Search by Analyzing User Intent
Does this still work for you? If so, are you using the most up-to-date WP version?
I ask this because I cannot get the simple RewrireRule to fire. It does not work on my development site or my production server.
Jeff Sayre´s last blog ..I’m BuddyPress-ed for Time
Hi Jeff
Yes, them rewrite rules still work for me and I’m on the latest WP already. You can check it out via the following links: http://kodeexii.net/b/1806 – a blog post
http://kodeexii.net/p/727 – a page
Are the rules still in your .htaccess file after the recent WP update?
Yes, soon after I posted, I realized that I could answer both my questions by viewing the browser source of your page.
The rules are in my .htaccess file. For some reason, this does not work on both my development site or my production site. The first, I’ve setup from scratch and can control the Apache configuration. The second, I have no control over. Yet, the results are the same. I’ve even tried this with the default Kubrick theme–just in case it was my custom theme. Finally, I deactivated the few plugins that I use to see if one of them could be causing an issue. Yet, no luck.
I’ve been kicking this problem around with an Apache expert on a support forum, but he’s as baffled as me.
Jeff Sayre´s last blog ..OAuth, BuddyPress, and Privacy
Hi Jeff
Have you ever used any plugins that could have changed how WP deals with redirections?
Apart from that, I’m not sure what could be causing your problem. Have you tried on a vanilla install of WP?
On my development site, I do not have any plugins activated. Also, I have tried a clean install (new DB, new WP files, and using the default theme) with the same results.
But, I have figured out that, at least for me, your method works with your custom permalink settings only (category/postname), and not with any of the other preset pretty permalink options. By the way, you should read this WordPress Codex page about starting permalinks with the %category% tag.
http://codex.wordpress.org/Using_Permalinks#Choosing_your_permalink_structure
Skip down to the “Structure Tags” subsection and read what it says about the %category% tag.
Thanks again for your suggestions!
So, this shortcode thingy I’ve got works only with the category/postname permalink structure eh. Hmmmm.. Great! Come to think of it, I’ve never tested it out on my other blogs though. They have postname permaling structure.
About that category permalink, yes, I’ve read about the performance issue thingy a while back. Didn’t change it back then since I didn’t want to break my offsite links. Didn’t know about anything about redirection back then. Thanks for reminding me about it. Will look into it soon. Then perhaps test out the shortcode thing on the new permalink structure.