Monday, June 30, 2008

Pretty URL for MediaWiki

MediaWiki is a very popular open source platform for wikis. The most notable user (and actual developer) being the infamous Wikipedia.

As I have previously mentioned, we run a MediaWiki based wiki within my research group. As a purely aesthetic function I chose to use a pretty URL. This essentially means the URL I use to access the wiki is shorter,
www.domain.com/wiki/Chemistry
instead of:
www.domain.com/wiki/index.php/Chemistry
It turns out it is quite fiddly to get the setup sorted, but the end result is definitely preferable.

N.B. These instructions only work when using domain.com/wiki/Chemistry. If you use wiki.domain.com/Chemistry that is more tricky and domain.com/Chemistry is not recommended, see here for info on those scenarios. In addition, I assume you have root access to your webserver, you are using MediaWiki 1.9.x or later and an apache webserver.

In this example I assume your DocumentRoot for html files is /srv/htdocs. Move your unpackaged mediawiki files to /srv/htdocs/w. We will use apache to make wiki valid.

Edit your httpd.conf, to enable /srv/htdocs to read .htaccess files. Alter AllowOverride None to AllowOverride FileInfo.

Now create the file to read /srv/htdocs/.htaccess, with the contents:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Make the wiki appear to come from wiki/
RewriteRule ^wiki/?(.*)$ /w/index.php?title=$1 [L,QSA]
Next tell your mediawiki configuration about this change by adding/editing /srv/htdocs/w/LocalSettings.php with:
$wgScriptPath       = "/w";
$wgArticlePath = "/wiki/$1";
Now when you access domain.com/wiki/Chemistry, Apache will actually request domain.com/w/index.php?title=Chemistry. Although you will not see this in the address bar unless you edit a page. If you edit a page but it wants to edit a page called Index.php, something has gone wrong!

Do check here for other information, perhaps the details there are sufficiently up to date to get this work - they were not when I originally tried.

There is one restriction with this method - articles names must not include a question mark (ampersands, &, are fine).