URL rewrite in IIS
From Meta, a Wikimedia project coordination wiki
| This Wikimedia page is of unknown usefulness and may be a candidate for deletion. If you want to revive discussion regarding the subject, you may try using the talk page or start a discussion at Babel. |
Rewriting URLs is a two phase process of beautification and translation. Implementing URL rewriting allows a user to type www.host.com/wiki/articleTitle and get results for www.host.com/w/index.php?title=articleTitle. (Where /wiki/ is the part of the URL to your wiki, and /w/ is the installation directory of index.php.) To understand the concept, it is critical to realize that the beautification process only affects the URLs that are displayed, not the real locations of the files and scripts that make Mediawiki work. The complementary translation process reverses the beautification process behind the scenes so the files and scripts are still called from their real locations.
Contents |
[edit] Beautification
In Mediawiki, beautification is handled by the wiki software. The assumed goal of beautification for this article is to have Mediawiki craft links according to a more intuitive format than the default which uses convoluted query strings. Default link formats can be overridden in LocalSettings.php. Links to articles are ultimately controlled by $wgArticlePath and are by default formatted like /w/index.php?title=articleName. A more intuitive format, such as that used by Wikipedia is /wiki/articleName. If $wgScriptPath is set to its default, a Wikipedia-style article link can be achieved by setting $wgArticlePath to $wgScriptPath/$1. The exact line in LocalSettings.php would be:
- $wgArticlePath = "$wgScriptPath/$1";
The $wgActionPaths array controls URLs of the tabs and buttons in the Mediawiki interface. The syntax for setting array values is different from other settings in LocalSettings.php. The proper syntax is $wgActionPaths['itemName'] = "path". Each item in the array is configured with one line in LocalSettings.php. Valid items for $wgActionPaths are:
|
|
|
|
|
|
With article links formatted according to the Wikipedia style, action paths formatted like /wiki/articleName/actionName provide quick access to article actions from the address bar. To achieve that, set the path for each $wgActionPaths item to $wgScriptPath/$1/actionName, assuming $wgScriptPath is set to its default. The exact line for one array item in LocalSettings.php would be:
- $wgActionPaths['view'] = "$wgScriptPath/$1/view";
[edit] recent changes and cache
Note that both your browser and the Mediawiki cache can prevent pages from reflecting the most recent changes in LocalSettings.php.
To clear the Mediawiki cache for a particular article, use the purge action in the page URL.
For example, if you want to get an all new version of the article Test Page: enter:
www.yoursite.dom/pathToWiki/index.php?title=Test_Page&action=purge.
This will clear the Mediawiki cache for that article and display the newly rendered article.
[edit] Translation
Translation converts beautified URLs back to their default formats and is handled by the web server rather than by Mediawiki. Apache is the only officially supported web server for Mediawiki. See Eliminating index.php from the URL for instructions on rewriting URLS in Apache. Although not officially supported, Mediawiki runs well in an IIS environment. Translating URLS in IIS requires an ISAPI filter. ISAPI filter installation requires administrative access to IIS. Several ISAPI rewriting filters are available.
Popular ISAPI URL rewriters
(from URL Rewriting in ASP.NET at MSDN)
Fortunately, ISAPI_Rewrite offers a lite version that is free and has all the functionality required to rewrite beautified Mediawiki URLs for the vast majority of installations. ISAPI_Rewrite allows URLs to be rewritten based on Regular Expressions. Download and install the filter according to the ISAPI_Rewrite documentation. RTFM. Once correctly installed, add RewriteRule directives needed to translate any beautified URLs to pathToISAPI_RewriteInstall/httpd.ini. Proper translation of the beautified article and action links discussed previously requires the following directives in httpd.ini:
[ISAPI_Rewrite] # Please replace "/wiki/" with the URL part of your wiki, and "/w/" with the install directory of your wiki # Keep the following rule if your CSS files are accessed through the same base URL as your wiki documents # Replace "skins/" in the following line with the folder to your CSS files RewriteRule /wiki/(?!skins/|images/)(.+?)(?:(?:\?)(.+))? /w/index.php\?title=$1&$2 [I] # If your CSS files are accessed through another URL than the prettified URL of your wiki, # then disable the previous rule and enable the following. #RewriteRule /wiki/(.+?)(?:(?:\?)(.+))? /w/index.php\?title=$1&$2 [I] # The following line does not seem needed anymore in current versions of MediaWiki: # RewriteRule /wiki/(?!Special)([\w:]+)/((?:view)|(?:watch)|(?:unwatch)|(?:delete)|(?:revert)|(?:rollback)|(?:protect)|(?:unprotect)|(?:info)|(?:markpatrolled)|(?:validate)|(?:render)|(?:deletetrackback)|(?:print)|(?:dublincore)|(?:creativecommons)|(?:credits)|(?:submit)|(?:edit)|(?:history)|(?:raw)|(?:purge))(?:\?(.+))? /w/index.php\?title=$1&action=$2&$3 [I] RewriteRule /wiki/(Special:[\w:]+)/(.+) /w/index.php\?title=$1&target=$2 [I]
Where /wiki/ is part of the URL pointing to your Wiki, and /w/ is the directory where index.php is installed. See the ISAPI_Rewrite documentation for a detailed explanation of these rules.
Also, IIS Mod-Rewrite provides high compatibility with apache mod_rewrite, both in configuration syntax and behavior. Additionally, the "Pro" version supports per-directory and override (.htaccess) configurations.