Easy Guide To Create Your Own URL Shortener
Friday, May 14, 2010 11:15 | Short URL for this post: http://hud.gs/xygmqQuick disclaimer
I use “easy” as a realtive term when describing this guide. Setting up a custom URL shortener in the way I’m about to describe is no more difficult than setting up WordPress on your own hosting, so if you managed that, you should be fine.
Stage 1 – Get a domain
This is the key thing really; you need to register a short domain. In my view, you should try and register a short domain that represents you or your business, rather than a random selection of characters. I went for hud.gs which ties in nicely with my surname. I usually use UKReg to purchase domains, but they don’t have a great range of country extensions. So instead I went with Go Daddy who have a large selection of country domain extensions available. Now look, I’m not advocating hosting with Go Daddy, but for the purposes of domain registration, they’ll do just fine.
Stage 2 – Host your domain
You’ll need a hosting account to host your shortener. If like me you run your own server (or even a VPS/reseller package) then you probably already know how to do this in WHM/Plesk or whatever you use. If not, you can get a very cheap hosting account from any number of places. I’ve used Tsohost for small content sites/blogs before and never had any problems. You’ll need PHP, MySQL and check if the host you choose has Curl installed (I would think all paid hosts have this), as this is needed too. Just sign up with your new domain name then go back to Godaddy and update the name servers for your domain to those provided by your host.
Stage 3 – Install YOURLS
Other free PHP/MySQL URL shortening scripts are available, but the reasons I chose YOURLS were:
- It’s still being maintained
- It comes with an API
- The creators had the foresight to create a WordPress plugin for it (more on that later)
Hint:
The YOURLS website doesn’t work in IE (doh!) so use a different browser
Download the script from here, extract and upload to the root of your new short domain. Create a database in your control panel and then follow the provided install instructions, including editing the config.php file with your newly created database access credentials.
Hint:
Chmod your root .htaccess file to 777 before running the install script to make life easier (make sure you change the permissions back after you’ve finished).
Then just run the install script and hey presto, YOURLS will be installed. Now there are additional steps I took with my installation that you don’t have to do, but you might want to if you’re competent with these kind of things:
- I created 2 username/password pairs so I could use a dedicated login for API access later
- I added .htaccess password authentication to the /admin directory, just as an extra layer of security
- I redirected the www domain version via .htaccess (quick guide here)
- I set up an index page at http://hud.gs/; you could also just redirect this.
Stage 4 – Change YOURLS short URLs (Optional)
YOURLS is a really excellent script with a lot of features (including full dashboard tracking for clicks), but there was one thing that bugged me. You can of course specify your own short URL when shortening via the admin system, but if you don’t, then it defaults to setting up a URL on an incremental value basis. Just to explain, the first URL I set up was http://hud.gs/1, the next was http://hud.gs/2 and so on.
I don’t know why but I just didn’t like this; I was expecting to see Bit.ly style random letter combinations. Quite a few people had the same thoughts as me as there’s no option to change this in YOURLS, although it might come in a future version. For now, I couldn’t see anything online about how to do this, so I came up with my own horrible code
Open up functions-baseconvert.php from the /includes directory and replace this code (found at the bottom):
-
if (!function_exists('bccomp'))
-
return base_convert($iNum, $iBase, $oBase);
-
-
if ($iBase != 10) $oNum = yourls_base2dec($iNum, $iBase, $iScale);
-
else $oNum = $iNum;
-
$oNum = yourls_dec2base($oNum, $oBase, $iScale);
-
return $oNum;
-
}
with this code:
-
-
/*****************************************************************/
-
//Quick random string hack – Jon Hudghton – www.hudghton.co.uk
-
$jh_l = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
-
$jh_r = '';
-
while (strlen($jh_r) < 3) { $jh_r .= $jh_l[rand(0,strlen($jh_l))];}
-
/*****************************************************************/
-
-
if (!function_exists('bccomp'))
-
return $jh_r . base_convert($iNum, $iBase, $oBase);
-
-
if ($iBase != 10) $oNum = yourls_base2dec($iNum, $iBase, $iScale);
-
else $oNum = $iNum;
-
$oNum = yourls_dec2base($oNum, $oBase, $iScale);
-
return $jh_r .$oNum;
-
-
}
Quite simply this will add three random letters (and now numbers) to the start of any auto-generated URL. Lowercase conversion is already handled at a later point by the script, as is checking if the auto-generated URL already exists. So, just overwrite the existing functions-baseconvert.php file on your server once you’ve made the changes and hey presto, your URLs will look a whole lot nicer
Stage 5 – Set up the WordPress plugin
As I mentioned earlier, one of the great things about the script is the provision of a WordPress plugin. You can download the plugin here and it’s installed in the same way as other WordPress plugins. Once you’ve installed it, go to Settings -> YOURLS in your WordPress admin to set it up:
Simples really. YOURLS can create a short URL for you automatically when you create a post, which is very useful. It can even connect to your Twitter account and automatically tweet your custom URL when you post. I don’t use this feature as I prefer to tweet my posts via Tweetmeme, but it’s there if you need it. The great thing about this API setup of course is that you can use the same shortener from multiple blogs. Very cool
Stage 6 – Hack Tweetmeme
Like a lot of people, I use the Tweetmeme WordPress plugin on my blog. Now obviously, if people are clicking that to tweet my posts, I don’t want them to be tweeting a Bit.ly short link (or some other service), I want them to be tweeting my short link, otherwise what’s the point? The trouble is, Tweetmeme doesn’t provide any option for this in their WordPress plugin:
Hmmm, no option to use my own custom URL there. Now this is one of those things I could have figured out myself, but fortunately someone else had already done it for me
Full credit to [I couldn't see a name on the about page] the guy at Pyn.me as he posted a solution to this very problem on his blog. For those of you who care, the Tweetmeme API supports an alias parameter that allows you to override the short URL being passed. So, simply open up the tweetmeme.php plugin file and after the line // what shortner to use add the following code:
-
$button .= '&alias=' . urlencode($shorturl);
Save and re-upload the plugin file. Now when you or anyone else clicks your Tweetmeme button, your short URL will be used, instead of that from another serivce. Magic.
Hint:
If you update your Tweetmeme plugin, this change will be overwritten so remember to make this change again any time you update the plugin file
You could probably write another plugin or something so this change remains permanent even when you update Tweetmeme, but for 2 lines of code it’s hardly worth it (for me anyway).
That’s it!
See, I told you it was easy. You now have a fully functioning URL shortener that you can use with all your WordPress blogs. And not only that, but it also works properly with your Tweetmeme button too. Magical stuff. If you have any issues I can try and answer them in the comments below. If you like this post, please retweet
Trackbacks/Pingbacks
- Tweets that mention Easy Guide To Create Your Own URL Shortener -- Topsy.com (May 14, 2010 at 11:25 am)
- Yourls 1.5 Shortened URL Length Hack « Sandy Dolphinaura (November 21, 2010 at 3:58 am)







So, I’m using Bitly Pro as you say. How do you hack tweetmeme if you’re doing that – presumably you put something else in:
$shorturl = get_post_meta($post->ID, ‘yourls_shorturl’, true);
Any idea what? I couldn’t find any documentation on the tweetmeme API alias.
I’m not sure on code for that, sorry. I’ll have a think and see if I can figue it out. Just to check, are you running the latest version of the Tweetmeme plugin (1.8.5)? I know 1.8.4 and 1.8.5 added more support for Bit.ly Pro and fixed the issue of not being able to save your API key if you selected Bit.ly as an option on the Tweetmeme admin page.
It was as simple as upgrading to 1.8.5. Now seems to be working … Cheers!
1.what?! – let’s just I’m behind. But I’ll upgrade if it helps … Could I tempt you with a subscribe-to-comments plugin in the meantime …?!
Good idea on the plugin – will look into that, Glad you got it working!
Wow, thanks for sharing this. Will try it out very soon
Works
Possible to add numbers too? What if I wanted to have something like xH54aS ?
Yeah, just include 0123456789 as part of the $jh_l string and add 10 to the rand function limit (i.e. 0,61).
Thanks a lot for the “random letter combinations” bit
I can’t get it to use uppercase letters…
This is set in the YOURLS config.php file. I’d recommend reading the documentation and the online help for YOURLS; it’s very useful
oh mate, sorry for that LOL i am just too busy
No worries; hope you get it all working as you want it!
Yes, mate. Will change the value in config.php this evening
Thanks for helping me!
Jon, Thanks a lot!
Save me couple of hours.
Hi Jon, very useful post! But “Stage 4 – Change YOURLS short URLs (Optional)” doesn’t seem to work in YOURLS V 1.5. In this version functions-baseconvert.php is missing.
Do you happen to know a solution?
Not installed 1.5 – I believe it now supports plugins and one of the available plugins allows the creation of random URLs.
Ok, thanks for the reaction. Haven’t seen that plugin yet. But will look for it.
I i just want to leave a simple comment to state your weblog was good. I came across it on yahoo lookup right after going through a great deal of other information that has been not really relevant. I was thinking I might come across this much earlier considering how excellent the content is.