Nginx short urls for mediawiki

mediawikinginxurl

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?

Best Answer

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.

Related Topic