Creating a URL shortener with MediaWiki

Today I tried out the new MediaWiki extension ShortUrl, that is intended to be used (it’s still in beta) on some Wikipedia editions, and therefore probably quite well coded. Using it together with some server rewrite rules, and Semantic MediaWiki, we now have a short URL for each article, printed at the bottom of relevant pages, like here (next to the QR code) in säsongsmat.nu/ssm/Potatis:

sasm.at/244 (11 characters) will now redirect to säsongsmat.nu/ssm/Recept:Sl%C3%A5nb%C3%A4rssaft_%C3%A0_la_Gusem%C3%A5la (71 characters), etc.

This is how it was done:

First, we need an short domain (in our case sasm.at), with the DNS set up to point to our site. We also need access to .htaccess or even better httpd.conf on our server, we need of course to be able to install extensions to our MediaWiki installation, and we need to be able to run MySQL commands on the wiki’s database.

  1. Install the MediaWiki extension ShortUrl as usual, by dropping it in the extensions folder and including it in LocalSettings.php: require_once( "$IP/extensions/ShortUrl/ShortUrl.php" );
  2. In LocalSettings.php, below the require_once-line for ShortUrl, add $wgShortUrlPrefix = '//shortdomain.com'; (in our case it would be $wgShortUrlPrefix = '//sasm.at';).
  3. Run the maintenance script php maintainence/update.php
  4. Now you need to edit your httpd.conf file or your .htaccess file, whichever you have access to. The ShortUrl extension works by creating a special page (Special:ShortUrl), from which pages can be reached by their short url ID like this:säsongsmat.nu/ssm/Special:ShortUrl/52
    What we want to do is to redirect all traffic from the short domain to this page. We also need an extra rewrite condition to make sure that we are not sending the vistitors into an infinite loop. It might look something like this (there is probably a better way, please let me know if you are good with these things!):

    RewriteCond %{HTTP_HOST} ^shortdomain.com
    RewriteCond %{REQUEST_URI} !^\/w\/index\.php.*$
    RewriteRule ^(.*)$ /w/index.php?title=Special:ShortUrl/$1

  5. The short urls that you get from the toolbox should now be working. Now if you want to print the short urls in the page, you have a couple of options:
    • If you know a bit of php you could edit your skin to do that for you, using the method ShortUrlUtils::encodeTitle( $title ), and e.g. print a link at the bottom of each page.
    • If you want to use the links inside the actual page, and want to try developing a small MediaWiki extension, you could write a parser function doing that (i.e. creating a new parser function to be used like this: {{#shorturl:Pagename}}). I’m sure more people would be interested in using it.
    • If you are running Semantic MediaWiki, the extension SemanticExtraSpecialProperties (version >= 0.2.3) now support the short url extensions, adding an extra special property for all pages with a short url. This is how we do it. The special property can be displayed just like any other property: {{#show:{{FULLPAGENAME}}|?Short URL}} (the property name will vary depending on your language settings).

Edit: The short urls are now shown by the skin instead, among the share buttons in the bottom of each article.

Edit 2: No need to run the database commands yourself, Jon Robson (Jdlrobson) points out that you just need to run the MediaWiki update script: php maintainance/php.php. I have updated this post to reflect that.