Manual:Short URL/Apache

These instructions help setup Short URLs on Apache. For information on what Short URLs are or to get instructions on configuration for other servers see Manual:Short URL. If you don't know what webserver you're using since you're using some sort of web hosting service, it's very likely that your host is using Apache.

These configurations have been codified into an external tool at http://shorturls.redwerks.org/. If you just want your wiki configured quickly and your wiki is publicly available (it does not work for private and firewalled wikis) then you may want to try that tool first.

Before setup
Decide what name you want to use for the virtual path in which you want articles to appear.

There are two sets of paths you'll want to know while configuring your wiki's short URLs:
 * Your SCRIPT_PATH is the path to /index.php in the long URL you should see when visiting an edit page on your wiki. You'll need to use this path in some of the configurations.
 * If your host uses something other than .php to refer to PHP files (such as .php5) your SCRIPT_PATH will need to reflect this.
 * The SCRIPT_PATH will likely be the same as the value for $wgScriptPath in your LocalSettings.php automatically generated by the installer.
 * Your ARTICLE_PATH is the path in the URL for the short URL you want. If you want your urls to look like /wiki/Article then your ARTICLE_PATH is "/wiki". Note that in $wgArticlePath we use $1 to refer to the title so for "/wiki" your $wgArticlePath would be "/wiki/$1".

Make note of one thing. If you've installed your wiki at the SCRIPT_PATH "/wiki/index.php" and are trying to get an "/wiki/Article" style ARTICLE_PATH you've installed your wiki incorrectly. "/wiki/Article" style URLs are done by installing at either "/index.php" or "/w/index.php". If you've installed at /wiki/ then you should go back and reinstall or move your wiki before continuing.

Root access, Apache configs
The recommended way to setup short URLs in Apache is by editing the config files. This requires that you have access to the server configuration. If you are on a shared host, you most likely don't. If you don't have root you should skip to the .htaccess setup below.

Finding your config file
The correct config file to edit may be in one of a number of places.

Most linux distributions setup Apache with set of sites-available/ and sites-enabled/ folders where VirtualHost blocks are setup. The correct config file to edit is the one in /etc/apache2/sites-available/ where the configuration for your wiki has been setup. If you haven't set one up and are using the default /var/www for your wiki setup then you can edit /etc/apache2/sites-available/default.

If your distribution does not setup sites-available/ or sites-enabled/ folders then you should edit the Apache configuration file directly. This file should be at /etc/apache2/apache2.conf. Note that it used to be named httpd.conf, if you have a httpd.conf and no apache2.conf then httpd.conf is the one you want to edit.

If your Apache config isn't in any of these spots you should consult the documentation for whatever system you used to install Apache to find the location of the Apache config to edit.

Setting up the alias

 * Ignore this section if you are using root URLs like /Article and /index.php, /somepath/Article and /somepath/index.php, or /Article and /w/index.php.

Find the directory in which your wiki was installed. When configuring the "Alias" line need absolute paths to your files. So you'll want to know what path on the filesystem your /index.php is located at. This is the same folder you put LocalSettings.php inside of. This should be somewhere in the DocumentRoot for your wiki. If you haven't done any customizations the default DocumentRoot on some linux distributions is /var/www. This can all be a little confusing, because on some systems if you install using the package manager (e.g., Debian; However using package managers to install MediaWiki is strongly recommended against), the path to your wiki may actually be a symbolic link (e.g., to /usr/share/mediawiki/).

The Apache config you need to add consists of a single line. An example is:

You'll need to tweak most of this line. /wiki should be your ARTICLE_PATH without any trailing slash. /path/to/index.php should be the absolute path on the filesystem to MediaWiki's index.php. This will likely be something like your DocumentRoot and SCRIPT_PATH combined together.

This line takes your article path and turns it into a virtual path pointing to index.php.

After you've setup the config inside Apache you're going to need to restart Apache to make it apply the new config.
 * If you are using Plesk or cPanel it should have a method of restarting the server.
 * From the command line the command is usually something like,  , or  . These commands need to be run as root, usually by prefixing them with.

Root urls (/Article)

 * Ignore this section if you followed "Setting up the alias".

If you are using root URLs where your ARTICLE_PATH is such that MediaWiki files like index.php fall under it (such as /Article and /index.php) then it's not possible to use Alias to setup your short URLs. You will have to setup root urls using the same method as used in .htaccess files to setup root URLs. Note that RewriteRules work inside of Apache config so you do not need to create a .htaccess file. Just follow the instructions in Setting up the rewrite rules but putting the config inside of your Apache config file instead of a .htaccess file.

.htaccess
If you don't have root access to configure your webserver you'll need to use .htaccess files to setup short URLs. .htaccess files are less efficient and don't give you as much control when it comes to fancy setups with multiple domains but they are often the only way to setup short urls on shared hosting.

For this short URL configuration you'll need to create a .htaccess file in a path that encompass both your SCRIPT_PATH and your ARTICLE_PATH.
 * If your SCRIPT_PATH is /w/index.php and your ARTICLE_PATH is /wiki/Article then you'll need to setup a .htaccess at /.htaccess
 * If your SCRIPT_PATH is /index.php and your ARTICLE_PATH is /wiki/Article then you'll need to setup a .htaccess at /.htaccess
 * If your SCRIPT_PATH is /index.php and your ARTICLE_PATH is /Article then you'll need to setup a .htaccess at /.htaccess
 * If your SCRIPT_PATH is /mediawiki/index.php and your ARTICLE_PATH is /wiki/Article then you'll need to setup a .htaccess at /.htaccess
 * If your SCRIPT_PATH is /foo/index.php and your ARTICLE_PATH is /foo/en/Article then you'll need to setup a .htaccess at /foo/.htaccess
 * If you want to redirect  without any path then you'll need to setup a .htaccess at /.htaccess even if your SCRIPT_PATH and ARTICLE_PATH are deeper.
 * [...]

If you already have a .htaccess there because you have multiple wikis then you'll have to do some work combining the .htaccess files together. For the most part the only thing you should keep in mind is that you don't need to put "RewriteEngine On" in the .htaccess file twice. Besides that you should be able to just add the new rewrite rules to the file.

Setting up the rewrite rules
The first rule you'll need inside of your .htaccess is one to enable the rewrite engine:

For normal short urls you will need a line that looks something like this to make your article path a virtual path pointing to index.php. The /w/index.php should be your SCRIPT_PATH. While the "wiki" in "^/?wiki(/.*)?$" should be your ARTICLE_PATH with no leading or trailing slash.

If you are using a root url instead of a normal short url in a nonexistent path you will need a slightly longer block to ensure that existing files are not rewritten.

For absolute root urls (/Article) this is simply. Where /index.php again is your SCRIPT_PATH.

For subpath root urls (/foo/Article while having a /foo/index.php) this would be: Where /foo/index.php is your SCRIPT_PATH and the "foo" in  is your ARTICLE_PATH with no leading or trailing slash.

If your wiki is not installed in the root (eg: /w/index.php and /wiki/Article) and you want / to redirect to your wiki add this to the .htaccess: Where /w/index.php is your SCRIPT_PATH.

Setting up LocalSettings.php
When you're finished with the server or .htaccess config there is a small bit of changes you need to make to LocalSettings.php to enable the short urls.

Setup your $wgArticlePath to the ARTICLE_PATH you decided on before. Make sure to include the $1 in the setting. It doesn't matter where you put the setting but it's a good idea to put it near the other path settings like $wgScriptPath Some examples:

$wgArticlePath for "/wiki/Article" paths:

$wgArticlePath for root "/Article" paths:

$wgArticlePath for "/en/Article" paths:

If you get an "Internal error" page saying "Redirect loop detected!" after you finish configuration you may be using something other than mod_php. If so you need to explicitly turn on short urls using the following line in your LocalSettings.php: