Configuring Short URLs in MediaWiki

Short URLs dispense with the index.php/ part of MediaWiki page URLs. If the MediaWiki instance is located in a subfolder (http://foo.ucla.edu/wiki) then Aliases can be used. If the MediaWiki instance is at the web root (http://wiki.foo.ucla.edu) then Rewriting must be used.

Aliases

In this method, the folder for the MediaWiki instance cannot be the same as the virtual folder used for the URL. For instance, let’s say we want our base URL to be /wiki. The operating system folder should be named something different, like /w. In the MediaWiki instance’s LocalSettings.php file, the following variables need to be adjusted:

$wgScriptPath	    = "/w";$wgArticlePath      = "/wiki/$1";

Then in Apache’s httpd.conf file the following entry must be added:

Alias /wiki "/path/to/wiki/w/index.php"

Once Apache is restarted, pages URLs become something like http://foo.ucla.edu/wiki/Main_Page. Script URLs (which are not normally bookmarked) become something like http://foo.ucla.edu/w/index.php?title=Main_Page&action=edit.

Rewriting

In the case where you want the pages to come right off the web root an alias cannot be used (since the names collide with script pages). In this case use rewriting:


RewriteEngine On
  1. Exclude actual file path and robots.txt (otherwise this would be a wiki page).
  2. An alias cannot be used because there’s not a unique path.
  3. Note this cannot go in the section since it would cause a loop.
    RewriteCond %{REQUEST_URI} !^/robots.txt [NC]
    RewriteCond %{REQUEST_URI} !^/w/ [NC]
    RewriteRule ^(.*)$ /w/index.php/$1 [L]

Put this in the <VirtualHost> section of httpd.conf. If you have rewrite rules for redirecting logins in the <Directory> section and put this rule there it may cause a loop. Any regular pages you want access will require a RewriteCond entry.

The MediaWiki settings are:


$wgScriptPath = “/w”;
$wgArticlePath = “/$1”;

Once Apache is restarted, pages URLs become something like http://wiki.foo.ucla.edu/Main_Page. Script URLs (which are not normally bookmarked) become something like http://wiki.foo.ucla.edu/w/index.php?title=Main_Page&action=edit.