888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

February 28, 2010

I’ve seen a few posts recently giving some basic ideas for rolling your own URL shortener. I guess these are all the rage. So I decided to have a go myself. I figured that WordPress had some behind-the-scenes stuff that could be used to good effect.

Before getting down to business though I want to ask why you want your own URL shortener. A few ideas occurred to me initially:

There are downsides though, of course. First, you need to develop something. The simpler the solution the more work will be required for each link; the more complex the solution the more time you will spend building it. You also have to forego integration with popular software such as TweetDeck.

How to build a simple shortner

The posts I have seen to date to do with short URLs tended to have two things in common. First, they assumed you wanted short URLs to your own posts. You might, but you might not. The second was that your domain name was already quite short. I make neither of those assumptions which is why step 1 is to get yourself a short domain name to use for your shortner.

Don’t worry about databases and stuff, the only thing I want you to put in the shortner domain is a htaccess file to redirect to your WordPress domain. Something like this:

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^shortdomain.com [nc]
rewriterule ^(.*)$ http://www.blogdomain.com/shortner/$1 [r=301,nc]

The effect of this should be to take ‘http://shortnerdomain.com/argument’ and change it to ‘http://www.blogdomain.com/shortner/argument/’. Exactly what we need to get WordPress to pass the user onto the right domain.

The WordPress bit

What we need WordPress to do with this information is:

This is pretty easy to do. Below is the complete code for the plugin. I will explain it underneath.

if (!class_exists(‘fw_links’)) {
class fw_links    {

/**
* PHP 5 Constructor
*/
function __construct(){
add_action(‘init’, array(&$this,’flush_rewrite_rules’));
add_action(‘generate_rewrite_rules’, array(&$this,’add_rewrite_rules’));
add_filter(‘query_vars’, array(&$this,’queryvars’) );
add_action(‘template_redirect’, array(&$this,’templateredirectintercept’))
}

/**
* Adds variables to retrieve from the query string
*/
function queryvars( $qvars ){

$qvars[] = ’shortnerlinkname’;
return $qvars;
}

/**
* Flushes the rewrite rules to force it to recrete them, including the new rules.
*/
function flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}

/**
* Adds the new rewrite rules to the list when the rules are reconstructed.
*/
function add_rewrite_rules( $wp_rewrite )
{
$new_rules = array( ’shortner/(.+?)/?$’ => ‘index.php?shortnerlinkname=’ . $wp_rewrite->preg_index(1));
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

/**
* Determine whether to load the template, and, set the is_dual_categories flag
*/
function templateredirectintercept(){
global $wp_query;

//get the link ID
$linkid = $wp_query->get(’shortnerlinkname’);

//get the links
$links = get_bookmarks(“category_name=shortner”);

foreach( $links as $link ){
if ( $link->link_description == $linkid ){

//do the redirection
wp_redirect($link->link_url);

//stop the script to prevent any more execution.
exit;
}

}
}
}

$fw_links = new fw_links();
}

This is what the plugin does:

First it tells WordPress to clear out the URL rewrite rules. These would have been created before the plugin could run so we need to retrigger the process. That processes generates all the rewrite rules so the plugin hooks into that and adds a new rule. The new rule converts ‘/shortener/argument/’ into ‘index.php?shortnerlinkname=argument’. You won’t see this happen, but it will.

The next step is to tell WordPress that we want to capture the value ’shortnerlinkname’. We do this by adding that to the queryvars array. WordPress now knows to store the argument in $WP_Query where we can get to it.

Finally we intercept the templating process. Get the argument. Get the bookmarks that are in the category named ’shortner’. Loop through them. If we find the one which has a description the same as the argument we use wp_redirect to send the user off the that link. Otherwise we do nothing and a 404 page will be displayed.

The end result

The end result is this:

  1. You create a new link / bookmark in the shortner category on WordPress with a description of ‘fun’
  2. You add this to your short domain and give someone the link ‘http://myshortdomain.com/fun’
  3. They type that in to their browser
  4. The .htaccess file redirects them to ‘http://myblogdomain.com/shortner/fun’
  5. WordPress processes that, finds the link / bookmark you created and sends the user to see Rick Astley.

Of course with some minor tweaks you could do more. For example, myshortdomain.com/p/argument could be redirected to myblogdomain?p=argument, but the basic concept will get you started.

Post Image The Easy Peasy Way
Get post images?
26 comments
page 1065
Html 5 Gallery
Html5 wordpress theme?
6 comments
page 1305
Post Image The Easy Peasy Way
Wordpress get first image large?
26 comments
page 1065
Using Wordpress As A Php Framework
Wordpress create your own framework?
2 comments
page 335
Improve Your Typography With Plugins
Wordpress typography plugin?
one comment
page 721
Quick N Dirty Replacement Text
Dirty replacement?
no comment
page 122
How To Add Sidebars To A Theme
How to add pages in footer wordpress?
11 comments
page 1053
Post Image The Easy Peasy Way
Get attachment by post?
26 comments
page 1065
Wordpress 25 Exif Fields
Exif info display wordpress?
12 comments
page 230
Using Your Own Url Shortener
Tiny urls htaccess?
4 comments
page 1190
Post Image The Easy Peasy Way
How to post all gallery images in one post wordpress?
26 comments
page 1065
Html 5 Gallery
Html 5 tab?
6 comments
page 1305
Six Million Ways To Die Choose One
6 million ways to die so i chose?
14 comments
page 1128
How To Add Sidebars To A Theme
Wp register sidebars?
11 comments
page 1053
Fun With Sidebar Tabs Styling
Funwithsidebartabs customize css?
2 comments
page 336
My Experience Of Flexx
Flexx theme review?
4 comments
page 1026
Wpunlimited The Ultimate Wordpress Theme
Ultimate admin theme?
3 comments
page 1141
Quick N Dirty Admin Login Screen
Css login screen?
no comment
page 128
Html 5 Gallery
Html 5 e wordpress?
6 comments
page 1305
Dont Mess With My Toot Toot
Toot html5?
16 comments
page 599
Fun With Sidebar Tabs Styling
Css tabcontentcontainer?
2 comments
page 336
Using Your Own Url Shortener
How to create your own url shortener?
4 comments
page 1190
Dont Mess With My Toot Toot
Wordpress custom content types?
16 comments
page 599
Using Your Own Url Shortener
Funny url shortener?
4 comments
page 1190
Theming Habari Vs Wordpress
How to create a habari theme?
13 comments
page 440
Post Image The Easy Peasy Way
Addd multiple images to post wordpress?
26 comments
page 1065
Upload From Url
Upload by url?
6 comments
page 326
Html 5 Gallery
Html5 image gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Add image url to page data wordpress?
26 comments
page 1065
Html 5 Gallery
Html 5 foto gallery?
6 comments
page 1305
Wpunlimited The Ultimate Wordpress Theme
Html 5 photo gallery?
3 comments
page 1141
Html 5 Gallery
Image galleries in html5?
6 comments
page 1305
Six Million Ways To Die Choose One
Six million ways to die?
14 comments
page 1128
  1 query every 1106 seconds, updated 1 seconds ago.
Wednesday, 1pm
 __
(__)
   `

 andrew

It can if you want to point to content on your own blog. If you want to use it for other things though you do need to do some coding.

To be honest, if you know what you are doing with htaccess then a good FTP editor is probably all you need if minimalism if your thing.

Wednesday, 12pm
 __
(__)
   `

 Tschai

It can even been done simpler and without any pluging: http://aytemir.com/need-a-short-url-service-try-wordpress/

This is a great advanced option, though…

Saturday, 7pm
 __
(__)
   `

 Create Your Own WP Based URL Shortener

[...] Rickmann has a slick post published on his Fun With WordPress blog where he explains how to create your own URL shortener using WordPress. You’ll need a short domain, some htaccess editing skills and a bit of php [...]

Friday, 8pm
 __
(__)
   `

 Nick

Nice tip! I was thinking about writing pretty much this exact plugin, but wanted to make sure it wasn’t out there already. Looks like you beat me to it.