User:Fo0bar/MediaWiki root articles

From mediawiki.org

One of my pet peeves is seeing wiki URLs such as http://www.example.com/wiki/index.php/Main_Page. There are many recipes out there for shortening that to "Wikipedia-style" URLs such as http://www.example.com/wiki/Main_Page, but I wanted to take it further. I wanted simply http://www.example.com/ to be the home page, and http://www.example.com/Other_Article to be a normal article.

Installation[edit]

  • Set up a normal MediaWiki installation in the directory /w under the document root.
  • Edit /w/LocalSettings.php.
  • Edit the virtual host's .htaccess.

LocalSettings.php configuration[edit]

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

.htaccess configuration[edit]

<IfModule mod_rewrite.c>
# Turn on mod_rewrite
RewriteEngine On

# If you are converting from a static site to a wiki,
# you'll want to make redirections to the new article.
RewriteRule ^old-file.html$ http://www.example.com/New_Article [R=permanent,L]

# This hack lets you essentially gets rid of the redirection
# to "Main Page".  Conversely, if you go to http://www.example.com/Main_Page,
# it sends an permanent external redirection to http://www.example.com/.
RewriteRule ^Main_Page$ http://www.example.com/ [R=permanent,L]
RewriteRule ^$ /w/index.php?title=Main_Page&redirect=no [QSA,L]

# Finally, if we've gotten this far, process it as if it's an article,
# UNLESS it's a physical file/directory/symlink.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /w/index.php?title=$1 [QSA,L]
</IfModule>

One step further: action URL rewrites[edit]

The above process makes the article URLs look pretty, but index.php is still referenced in most actions. To solve that, we can use mod_rewrite to map to better URLs, and tell MediaWiki to use them for actions. In this guide, http://www.example.com/w/index.php?title=Main_Page&action=edit becomes http://www.example.com/w/action/edit/Main_Page.

LocalSettings.php configuration[edit]

foreach($wgActions as $action => $value) {
  if($value === false) { continue; }
  if($action == 'raw') { continue; }
  $wgActionPaths[$action] = "$wgScriptPath/action/$action/$1";
}
$wgActionPaths['view'] = $wgArticlePath;

.htaccess configuration[edit]

Insert the following above the RewriteConds above:

RewriteRule ^w/action/([a-z]*)/(.*)$ /w/index.php?action=$1&title=$2 [QSA,L]