Skip to content


Very pretty URLs in MediaWiki

Finally, we did it, we put short URLs (aka pretty URLs) on Wiki4Games. The thing is, I had a hard time at this because I did it a way which seems to be definitely inadvisable according to MediaWiki’s short URL how-to. Basically the recommended method says that you need to place your MediaWiki files anywhere but in the root folder (e.g., wiki4games.com/w/) and that you also need to make your wiki run in a different, non-existing and not root virtual directory (e.g. wiki4games.com/wiki/). Not only this method will result in silly looking URLs but it also needs you to edit httpd.conf.

Potential issues

Anyway, I decided to try and find my own way to the much not recommended wiki4games.com/Page_Title URL scheme. Before going further, the potential problems with this are:

  1. It’s unsupported, so if it ever gets broken by a MediaWiki update you won’t get help from them to fix it.
  2. Your wiki won’t be able to have a page named like one of the core files. This shouldn’t create many problems, yet you should be aware of this before going further. The potential issues with this limitation are also reduced by the fact that any MediaWiki page name begins with a capitalized letter, while no core file does. So on a case-sensitive server (Linux… ;)) you can’t create a MediaWiki page named index.php but you can still create one named Index.php!
  3. All requests for non-existing files or folders will be forwarded to MediaWiki. For instance, if you don’t create a robots.txt file, a search engine looking for it will be forwarded to your wiki page “Robots.txt”. The fix is however simple enough: just create an empty robots.txt file. Same story for favicon.ico, etc. This may also lead to issues with badly crafted domain ownership validations, such as McAfee SiteAdvisor’s. Again, the fix is simple: don’t use such crappy services – would you trust a security consultant which isn’t even able to provide a reliable automated domain ownership verification?

The .htaccess part

First I leeched WordPress’s .htaccess file: my new .htaccess file is now:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [PT,L,QSA]
</IfModule>
#ErrorDocument 404 /404.php

In case you really don’t have a clue, this .htaccess file should be saved in the webserver’s root folder, which in our case is the wiki’s root folder. This file is explained in details there. Basically, it says that if the URL requested doesn’t point to a file (-f) or a directory (-d), Apache will load index.php.
The error document is our former pretty URL handler (we made it to redirect all 404s to index.php, so in a way pretty URLs existed but MediaWiki didn’t use it because it wasn’t configured for it), if the improved solution ever breaks we could still quickly fall back to this old 404. Here it is if you want it: http://www.wiki4games.com/User:Patheticcockroach/404.php.

It’s been reported that RewriteRule may encounter problems with special characters like ampersand, the fix for it would be to add a B flag to the rewrite rule. I.e.:
RewriteRule . /index.php [PT,L,QSA,B]
(NB: this would work too: RewriteRule . /index.php?title=$1 [PT,L,QSA,B]).
It’s also been reported that for some unspecified reason you need to add Options FollowSymLinks in the .htaccess file. I didn’t need to, but if you experience trouble you might as well try to add this line.

Now LocalSettings.php

Then I tweaked some LocalSettings.php lines:
1. I already had (and you should have this too if you’re on a wiki ran from the root directory of your web server), from the existing installation:
$wgScriptPath = "";
$wgScriptExtension = ".php";

These lines are fine, just let them this way.
2. The 2 settings I had to change were:
$wgUsePathInfo = true;
$wgArticlePath = "/$1";

Final thoughts

Well, that pretty much covers it. Upload the modified files and now your pages should work both as yourdomain.com/A_Page and yourdomain.com/index.php?title=A_Page. Like

Enjoy your very pretty URLs! 🙂 (which are more than pretty because they don’t contain a /wiki!)

PS: don’t forget to check that all your customizations still work, notably your logo on funky pages like subpages: the slash will break the relative path if you didn’t configure your logo properly, i.e. something like "{$wgScriptPath}/path/to/the/logo.png" or "{$wgStylePath}/common/images/logo.png"

Software used:
MediaWiki 1.15.1
PHP 5.2.x
Apache 2.2.x

Posted in MediaWiki.


3 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Philip says

    thanks, works like a charm
    the solution provided by mediawiki does not work with the 1.15 version

  2. patheticcockroach says

    Hm, I’m not sure that can be done using just using mod_rewrite. In some frequent cases (like, when you edit a page), you need to have the index.php?title=Beneath_a_Steel_Sky&action=edit form, so you can’t just blindly substitute it, you need to make sure there’s nothing more in the URL.
    Would be quite trivial to do in simple PHP, though (get all the GETs, and if there’s only the title redirect the page to the pretty URL version), but integrating this into MediaWiki is probably going to be a bit more painful.



Some HTML is OK

or, reply to this post via trackback.

Sorry about the CAPTCHA that requires JS. If you really don't want to enable JS and still want to comment, you can send me your comment via e-mail and I'll post it for you.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA