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