Table of Contents
In case you installed MediaWiki on IIS, you might want to use pretty URLs instead of ugly ones. To quickly understand what we're talking about, here's an example of pretty URL:
/wiki/sample_page
and here's an ugly one:
/w/index.php?title=sample_page
Here's a quick guide explaining how we can do that in few steps.
IIS and Web Application Setup
To enable pretty URL support under IIS, you need to do the following:
- Ensure that your web server has the URL Rewrite module installed.
- If you didn't do that already, move all your MediaWiki installation files within a subfolder of your web application's root folder, such as /w/.
- Create (if it doesn't exist already) a brand-new web.config file and place it within the web application root folder.
If you did everything properly, root folder of your web application should contain the web.config file only, being it the one that will handle the page redirects: all the MediaWiki files, including the LocalSettings.php configuration file, will be in the /w/ subfolder instead.
MediaWiki Configuration
Once done, open the aforementioned LocalSettings.php file and modify it to match the following values:
1 2 3 4 5 6 7 8 |
// The directory under your document root where MediaWiki is installed $wgScriptPath = "/w"; // This is the rewritten URL $wgArticlePath = "/wiki/$1"; // Set it to TRUE to enable pretty URLs, FALSE to use ugly URLs $wgUsePathInfo = true; |
This will enable pretty URL support for our MediaWiki: however, we still need to properly rewrite these pretty URLs requests, otherwise they will hit a 404 - Page Not Found HTTP error.
Web.Config file
In order to do that we need to properly configure the previously added web.config file to handle pretty URLs. Here's a working sample:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="wikiRule1" stopProcessing="true"> <match url="^wiki/(.*)$" /> <action type="Rewrite" url="/w/index.php?title={UrlEncode:{R:1}}" /> </rule> <rule name="wikiRule2" stopProcessing="true"> <match url="^wiki/$" /> <action type="Rewrite" url="/w/index.php" /> </rule> <rule name="wikiRule3" stopProcessing="true"> <match url="^/*$" /> <action type="Rewrite" url="/w/index.php" /> </rule> </rules> </rewrite> </system.webServer> </configuration> |
As we can see, the highlighted rules will rewrite each and every pretty URL request to their corresponding ugly one, so our index.php file will be able to take care of them accordingly and thus return the actual page in a transparent way. Needless to say, nothing of this will work if your IIS is lacking the URL Rewrite Module (see paragraph 1 of this post).
Colons bug in IIS 7.5
If you're running IIS 7.5 you might also incur in another issue that seems to happen under certain circumstances when an URL contains unescaped colons. The issue is well documented in this StackOverflow post, which also provides a decent workaround if you can't upgrade to IIS 8.0 or above (which fixes that for good):
Add the following in your MediaWiki's root web.config file:
1 2 3 4 |
<system.web> <!-- NOTE: This will fix the "colons" bug. See http://stackoverflow.com/questions/13004926/displaying-colons-in-mediawiki-urls-on-iis-7-5 --> <httpRuntime requestValidationMode="2.0" requestPathInvalidCharacters="<,>,*,%,&,\" /> </system.web> |
And also add the following JQuery snippet into the MediaWiki:Common.js special page of your MediaWiki installation:
1 2 3 4 5 6 |
/* IIS 7.5 colon bug fix, cfr. http://stackoverflow.com/questions/13004926/displaying-colons-in-mediawiki-urls-on-iis-7-5 */ $(function() { $('a').each( function() { this.href = this.href.replace(/%3A/g, ":"); }); }); |
If you never used the MediaWiki:Common.js page, you can read how it works on the official MediaWiki documentation or on this StackOverflow answer. In short words, it's a quick way to add a JavaScript snippet to every page without having to manually edit the MediaWiki source code or add additional resource files.
One Comment on “MediaWiki: how to handle pretty URLs under IIS with web.config and URL Rewrite module”