I am trying to do short URLs for a MediaWiki site. The wiki is in a subdirectory mydir (http://www.example.com/mywiki). I've already set up rewrites in /etc/nginx/sites-available so that example.com redirects to example.com/mywiki.

Currently the URL is like http://www.example.com/mywiki/index.php?title=Main_Page. I want to clean up the url so that it looks like http://www.example.com/mywiki/Main_Page. I am having quite a bit of trouble doing this. I am not familiar with regular expressions or the syntax that the nginx config files use.

This is what I currently have:

server_name example.com www.example.com;

location / 
  rewrite ^.+ /mywiki/ permanent;

location /wiki/ 
  rewrite ^/mywiki/([^?]*)(?:\?(.*))? /mywiki/index.php?title=$1&$2 last;

The second rewrite is obviously the one that's broken. It is based off of Page title -- nginx rewrite--root access in the MediaWiki documentation.

When I try to load the site, the browser tells me I get infinite redirects. Does anyone who how I should go about fixing this issue? Or rather, what is the correct way to implement this, and what do all those symbols mean?

  • Do you have a location for /mywiki/? – mgorven Jun 28 '12 at 5:45

That's a very interesting expression they have there. Their main manual page for short URLs has this to say:

These guides are old and are almost entirely bad advice.

Anyway, let's see if we can simplify it a bit.

rewrite ^/wiki/([^\?]*) /mywiki/index.php?title=$1&$args last;

Note that you can't overlay the 'pretty' path (which you've put in the location block, so let's run with it) and the physical path; the norm is to use /wiki/ as the pretty path (your $wgArticlePath config) and /w/ as the physical path (your $wgScriptPath config).

So, all in all, something like this:

location / {
  rewrite ^/$ /wiki/ permanent;

location /wiki/ {
  rewrite ^/wiki/([^\?]*) /mywiki/index.php?title=$1&$args last;

...and you'll need the PHP handling in some form as they've shown in their example, as well as to update your Settings.php with appropriate $wgScriptPath and $wgArticlePath settings, as well as setting $wgUsePathInfo to true.

| improve this answer | |
  • Awesome! That fixed the issue. It seems that I had some confusion over the LocalSettings.php stuff, and my paths were messed up. Thank you! – William Jun 28 '12 at 8:48
  • @William Nice - I'm actually a little surprised I got that right on my first try, last time I set this up I kept missing things! Maybe we should update their wiki page with the easier config ;) – Shane Madden Jun 28 '12 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.