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.

Html 5 Gallery
Wordpress html5?
6 comments
page 1305
Quick N Dirty Admin Login Screen
Login screen css?
no comment
page 128
What Wordpress Workflow Needs
Word press workflow?
3 comments
page 1226
How To Add Sidebars To A Theme
Wordpress thematic modify sidebars?
10 comments
page 1053
Post Image The Easy Peasy Way
Wordpress theimage?
26 comments
page 1065
Six Million Ways To Die Choose One
Ways to die without noticing?
14 comments
page 1128
Using Your Own Url Shortener
Htaccess tiny url shortner?
4 comments
page 1190
Photoshop Design Framework
Photoshop framework?
3 comments
page 296
Creating Custom Urls
Custom url for wordpress page?
6 comments
page 80
How To Add Sidebars To A Theme
How to add a sidebar to a theme?
10 comments
page 1053
Photoshop Design Framework
Framework in photoshop?
3 comments
page 296
What Wordpress Workflow Needs
Wordpress workflow?
3 comments
page 1226
Html 5 Gallery
Wordpress html5?
6 comments
page 1305
Using Your Own Url Shortener
Build your own url shortener?
4 comments
page 1190
My Experience Of Flexx
Cant upload image to flexx wordpress theme?
4 comments
page 1026
Post Image The Easy Peasy Way
Wpget post image?
26 comments
page 1065
Post Image The Easy Peasy Way
Get postattachments?
26 comments
page 1065
Html 5 Gallery
Html5 plugin wordpress?
6 comments
page 1305
3 Ways To Speed Up Your Blog Without A Cache Plugin
Wordpress cache disqus?
one comment
page 1321
Questions About Habari For Wordpress Users
Habari or wordpress?
6 comments
page 424
Custom Hooks For Admin Pages
Wordpress post form admin pages?
one comment
page 430
Updating Code Snippets Here
Yb fun blogs?
no comment
page 1338
Html 5 Gallery
Html 5 photo gallery?
6 comments
page 1305
Html 5 Gallery
Html5 photo gallery?
6 comments
page 1305
Using Wordpress As A Php Framework
Using wordpress as a framework?
2 comments
page 335
Using Your Own Url Shortener
Rewriterule shorten url?
4 comments
page 1190
Post Image The Easy Peasy Way
Finding attached image with post in wordpress?
26 comments
page 1065
Wordpress 25 Exif Fields
Wordpress exif plugin?
12 comments
page 230
Html 5 Gallery
Html 5 photo gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Wp image attached linkable?
26 comments
page 1065
My Experience Of Flexx
How to edit flexx theme wordpress?
4 comments
page 1026
Post Image The Easy Peasy Way
List image attachments wordpress?
26 comments
page 1065
Post Image The Easy Peasy Way
Wp get first image for post?
26 comments
page 1065
  1 query every 1054 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.