MediaWiki/Guide/Configure/Short URLs

From grantswebsite.net
Jump to navigation Jump to search


A Guide to using Short URLs for MediaWiki Pages

This article is part of a series compiled as a guide to encourage and assist those building a MediaWiki-based website in a hosted environment.

Each article links to relevant documentation from the MediaWiki.org website and the Wikimedia.org website. Where the official documentation does not adequately cover the issues for a hosted site, or is too 'advanced', additional information, explanation and advice is provided.

MediaWiki URLs look like this, by default:

     http://www.mywebsite.org/w/index.php?title=Page_title 

A Short URL hides the php file extensions from the page address, and replaces the installation path /w with a virtual directory /wiki (that doesn't actually exist), so the page URL typically looks like this:

     http://www.mywebsite.org/wiki/Page_title 

This Guide provides an explanation of how to achieve this result, particularly for MediaWiki sites in a hosted environment.


Step 1 - Install MediaWiki in a Directory /w

According to the MediaWiki Manual:Short URL "A common beginner's mistake is to install MediaWiki itself (the source code, not the short url) in /wiki instead of /w. Once installed they would realize the mistake when trying to configure Short URLs (which would become difficult as the virtual path then conflicts with the real path)."

If your MediaWiki site will be hosted by a web host service provider the MediaWiki application will be available from a selection of supported software. If you have the option of a custom install, choose that. When you have the option of entering the name of the installation directory (the field could be blank, or MediaWiki), change it to /w. The / refers to the root directory of your hosted website. Think of it as www.mywebsite.org/. /w will be a sub-directory.

When the installation is complete, you will need to make changes to the LocalSettings.php file, which is now in the /w directory; and create or change the .htacces file which should be in the root directory /.

Manuals and Links

Short URL


Step 2 - Configure LocalSettings.php

Add the following lines to the file LocalSettings.php:

# Short File Names
$wgScriptPath       = "/w";
$wgArticlePath = "/wiki/$1";


Any line beginning with # is treated as a comment.

$wgScriptPath and $wgArticlePath are variables: the value assigned to them affects the behavior of MediaWiki.

  • $wgScriptPath is the path to the MediaWiki files in your installation directory, which should be /w.
  • For $wgArticlePath the $1 is replaced by the name of your MediaWiki page or article. If your installation is in the sub-directory of your root folder, /w, and you want the URL of a page to show the path /wiki instead of /w, then set the value of $wgArticlePath to /wiki/$1.

To make it work though, your web server needs some instructions - see the next section.


Step 3 - Edit the Web Server Configuration File

If your MediaWiki site is hosted you will not have direct access to the web server, but you can tell it how to serve content from MediaWiki though a configuration file called .htaccess. This file should be in the root directory of your website.

.htaccess is a simple text file but it has no file extension.

The following instructions assume that your service provider is using Apache as a webserver.

The Manual:Short URL/wiki/Page Title -- .htaccess suggests that the .htaccess file should have the following content:-


RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^wiki/(.*)$ w/index.php?title=$1 [PT,L,QSA]
RewriteRule ^wiki/*$ w/index.php [L,QSA]
RewriteRule ^wiki$ w/index.php [L,QSA]


The commands tell the web server to re-write URLs so a typical MediaWiki page with a URL /w/index.php?title=Page_title will be re-written as /wiki/*$ where *$ takes the value of the page title.

The syntax is explained more fully in the Manual:Short URL/Apache.



Disclaimer

The information or advice provided in this Guide is based on, or links to, official documentation for MediaWiki and was accurate when this article was created. However, some variation may occur between versions of MediaWiki; and the specifics of web hosting varies by service provider. Consequently, you should always create an effective backup before making any changes; ensure that you can restore your database and website; read the Release Notes before upgrading; and apply best practices to the management of your website. Any action that you take based on information provided here is at your own risk and the author accepts no liability for any loss or damage.