YOURLS: Your Own URL Shortener
October 24th, 2010 2 Comments
Shorteners in brief
If you use twitter you'll be familiar with the concept of URL shortening. You want to tweet about that video where the dog thinks its own leg is trying to steal the bone but you've only got 140 characters to say what the video is and include the link to YouTube. URL shorteners allow you to change:
http://www.youtube.com/watch?v=tJgMueh-zLM&feature=youtu.be
to:
http://bit.ly/dfzFE6
Even if you don't use twitter URL shorteners can come in handy. For example, at the beginning and end of the ITauthor podcast I use some music by Amplifico and I like to put a link to their page on musically.com in the MP3 description that you can read on your iPod when you're listening to the podcast. It's much nicer to give the URL http://tinyurl.com/amplifico, rather than http://www.musicalley.com/music/listeners/artistdetails.php?BandHash=cdef1ecef0d12844ed816b922fcada5d.
Some popular URL shorteners
- tinyurl – This was the first URL shortener most of us will have come across - way back before twitter appeared and ramped up demand for short URLs, leading to a proliferation of shortening services.
- bit.ly – twitter supported use of bit.ly which made it a popular service. Recently there have been doubts raised about the wisdom of using a Libyan registered domain (.ly) as the Libyan government have said they will take down domains that contain immoral content.
- j.mp – This is just bit.ly but with 2 fewer characters. If you already have a bit.ly URL you can use the same shortened path, stick it on the end of the j.mp domain and save yourself those 2 precious characters. For example, the dog video gets shortened to http://j.mp/dfzFE6.
- goo.gl – Google are one of the many big companies that have now got into the URL shortening business.
- is.gd – just a nice simple Web page that produces nice short URLs.
- ... I could go on, but there's not a whole lot of difference between these services.
Your very own URL shortener
Shortening URLs isn't difficult to do and there are a selection of free URL shorteners that allow you to produce your own short URLs. All you need is your own Web site and your own domain name. So, for example, I own the domain name itauthor.com, so I can produce short URLs like http://itauthor.com/1 or (more descriptively) http://itauthor.com/podcast36.
The solution I'm using is called yourls. It's a series of PHP scripts with a MySQL database behind it. So if you're already running a Web site based on PHP and MySQL (for example, a WordPress blog) then you've already got everything you need. Just upload it and browse to the admin page. The yourls contains all the instructions you need.
The only problem I had was as a result of some changes not getting written to my .htaccess file in my root Web directory. I had to go and manually add the following at the start of the .htaccess file :
# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^itauthor.com$
RewriteRule . - [S=3]
RewriteRule ^([0-9A-Za-z]+)/?$ /yourls-go.php?id=$1 [L]
RewriteRule ^([0-9A-Za-z]+)\+/?$ /yourls-infos.php?id=$1 [L]
RewriteRule ^([0-9A-Za-z]+)\+all/?$ /yourls-infos.php?id=$1&all=1 [L]
</IfModule>
# END YOURLS
You don't need the two lines highlighted in red if you're not running WordPress, or anything similar that relies on being able to rewrite URLs. The yourls documentation says, in this situation, you need to put all the yourls files and directories in a subdirectory of your root Web directory (e.g. in a directory called "u"). However, this means that you need to include the subdirectory in the YOURLS_SITE configuration setting and it'll then be part of the shortened URL (e.g. http://itauthor.com/u/123, which kind of defeats the purpose. So the two red lines get around this by diverting URLs without "www" to yourls.
The first of the red lines says "only apply the following rule if the URL doesn't begin http://itauthor.com". The second red line says "if the previous condition resolved as true then skip the following three rules".
This seems a bit like a double negative but it's necessary because RewriteCond only applies to the RewriteRule that immediately follows it, so we need the skip rule. The result is that, on my site, the three RewriteRules that divert page requests to the yourls PHP scripts are only applied to URLs beginning http://itauthor.com. The "[L]" means "last" - in other words, if this RewriteRule is applied don't go any further, so we never reach the rules that WordPress uses, which are further down the .htaccess file. If a URL begins http://www.itauthor.com then the yourls rules are skipped and the URL is processed using the WordPress rules.
This means that http://itauthor.com/2 is sent to yourls to retrieve the original, long URL from its database, whereas http://www.itauthor.com/podcasts is sent to WordPress to create a Web page using content from its database.
What's the point?
Well, okay, there's really no point other than a bit of personal domain name vanity. Why have your tweets full of bit.ly or goo.gl URLs when you could have your own domain name showing up – even if clicking the link doesn't take your tweet readers to your Web site.
And to finish, just because I find it very funny, here's that video of the back leg bone thief:
Potentially similar posts
- ITauthor podcast #36 – Acrobat and shared review of Web pages – October 2010
- AudioBoo makes (mini) podcasting easy – September 2010
- Mag+ video: How to sell an idea – September 2010
- ITauthor podcast #33 – A history of RSS – March 2010
- About – November 2006

October 24th, 2010 at 10:19 pm (#)
Bah Humbug, I gave it a quick go and it just gave me a 404. I think I got the .htaccess file wrong because it also stopped all the images I'd uploaded to WordPress from displaying too.
I'll have a look at it later and see if I can sort it.
October 25th, 2010 at 10:25 pm (#)
Sorry to hear that didn't work for you Omar. Apart from having to enter those lines in the .htaccess file manually, I found it surprisingly easy to set up. Let me know if you figure out what the problem was.