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.



Wordpress feature image page?
24 comments on page 1065

Addsettingsfield?
3 comments on page 793

Shorter link?
4 comments on page 1190

Shorter link?
4 comments on page 1190

Cssimageforlogin?
no comment on page 128

Cssimageforlogin?
no comment on page 128

Get featured image url wordpress?
24 comments on page 1065

How to clear wpcache?
9 comments on page 1310

Photo gallry code html5?
6 comments on page 1305

Wordpress get image size?
24 comments on page 1065

Sitecouk wptheme?
2 comments on page 1376

Wordpress chat disqus?
no comment on page 1308

Howtocreategoodsizelayout?
4 comments on page 1026

Friend connect profile?
6 comments on page 1364

Get medium image from post?
24 comments on page 1065

Tabbed widgets wp?
no comment on page 333

Wordpress plugin post separator?
no comment on page 1371

Wordpress pictue cache lschen?
9 comments on page 1310

Display photos html 5?
6 comments on page 1305

Fun wordpress themes?
2 comments on page 1376

Wp featured image without size?
24 comments on page 1065

Wordpress get url global?
4 comments on page 1190

Wp unlimited?
3 comments on page 1141

Html5 image gallery?
6 comments on page 1305

Wordpress exif plugin?
12 comments on page 230

Css login screen?
no comment on page 128

Htlm 5 photo gallery?
6 comments on page 1305

Css login screen?
no comment on page 128

Comments by intensedebate?
7 comments on page 1175

Image gallery html 5?
6 comments on page 1305

Wordpress andrew rickmann?
2 comments on page 1376

Wordpress medium large?
24 comments on page 1065

Fun with uninstallation wordpress?
53 comments on page 100

How to clear wp cache?
9 comments on page 1310

Photoshop frameworks?
3 comments on page 296

Html5 gallery?
6 comments on page 1305

Html5 photo gallery code?
6 comments on page 1305

Wordpress post multiple images?
24 comments on page 1065

Html 5 photo gallery?
6 comments on page 1305

Login screen css template?
no comment on page 128
  every 1981s, 1s ago, in 0.02s.
 __
(__)
   `
 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.

 __
(__)
   `
 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…

 __
(__)
   `
 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 [...]

 __
(__)
   `
 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.


0.01s