Eliminating index.php from the url

From Meta, a Wikimedia project coordination wiki
This is an archived version of this page, as edited by Mattbrundage (talk | contribs) at 03:22, 5 December 2006 (→‎Using a rewrite rule in a .htaccess file: spelling). It may differ significantly from the current version.


The page describes the steps needed to eliminate index.php from the URL. The goal of this article to change viewing URLs to no longer include "index.php?title=", meanwhile edit pages and will still use "index.php?title=".

Overview

By default, URLs in MediaWiki will appear like this:

http://mywiki.site.tld/wiki/wiki/index.php?title=Article_name

This article deals with changing the URL to appear like this:

http://mywiki.site.tld/wiki/Article_name

However, editing pages under a changed system will still use URLs like this:

http://mywiki.site.tld/w/index.php?title=Article_name&action=edit

In LocalSettings.php, several lines of code are used to determine the make-up of the URL:

$wgScriptPath = "/w";
$wgScript = "$wgScriptPath/index.php";
$wgArticlePath = "$wgScript?title=$1";

$wgScriptPath determines the root wiki directory. In the case directly above, all of the MediaWiki files are in a folder named "w". $wgScript references $wgScriptPath and tells your wiki where index.php can be found. In this case, index.php can be found directly inside the "w" folder. $wgArticlePath tells MediaWiki the path to the articles. $wgArticlePath references $wgScript, which references $wgScriptPath telling MediaWiki that in this case, article URLs are in /w/index.php?title=Aritcle_name.

Depending on your individual system configuration, some of the methods listed may not be possible.

See also: Using a very short URL for instructions to eliminate even the "wiki" part of the URL.
See also: MediaWiki User's Guide.

Using aliases in httpd.conf

This is the prefered method from a performance point of view, but requires access to edit httpd.conf. Most shared hosting systems do not allow changes to httpd.conf. The method below has been tested successfully with MediaWiki 1.4.4.

1. In LocalSettings.php, make sure you are using these default values:

$wgScriptPath = "/w";
$wgScript = "$wgScriptPath/index.php";

If you put the wiki installation in a subdirectory such as /w, use $wgScriptPath = "/w" as appropriate.

If you put the files (such as index.php) in the root, you can use $wgScriptPath = "" in LocalSettings.php.

2. In LocalSettings.php, set the following:

$wgArticlePath = "/wiki/$1";

Remember, the virtual directory for the wiki article space should never, ever overlap or hide real files. In particular it should never, ever overlap your base installation directory or the root directory. It can be a virtual subdirectory, such as /wiki. (For example: do not try to rewrite "/wiki/Article" to "/wiki/index.php?title=Article).

3. Set up the following alias in your Apache httpd.conf. It can be in a <VirtualHost> section, or in "Aliases" section, or in your general site config. In this alias, the prefix /filesystem/path/to/my/site represents the path you installed to — the directory where MediaWiki's index.php lives. Replace the prefix as appropriate for your actual file system path.

#These must come last, and in this order!
Alias /wiki /filesystem/path/to/my/site/index.php
Alias /index.php /filesystem/path/to/my/site/index.php

For example:

Alias /wiki "c:/apache/www/w/index.php"
Alias /index.php "c:/apache/www/w/index.php"

After making modifications to httpd.conf, you might have to restart Apache to apply the changes.

Make sure Apache loads the Rewrite module. In httpd.conf this line must be added/uncommented:

LoadModule rewrite_module modules/mod_rewrite.so

After making modifications to httpd.conf, you might have to restart Apache to apply the changes.

If you are using Apache 2, you might also need to turn on AcceptPathInfo. It is on by default in a standard installation of Apache and PHP, but some vendors/packagers may have it disabled on your system.

Using a rewrite rule in a .htaccess file

This method can be used instead of edits to httpd.conf. .htaccess files allow the ability to command the Apache server when you're unable to edit the httpd.conf files. Using RewriteRules in a .htaccess file, you can tell the server to display URLs that don't include "index.php?title=". However, this method does require slightly more work for the server. Also, in order to use this method, the MediaWiki install must be placed in the "w" directory, not in any other folder, including a folder named "wiki". If you install MediaWiki into a folder name "wiki", you can simply rename the "wiki" folder to "w" and change your LocalSettings.php from $wgScriptPath = "/wiki"; to $wgScriptPath = "/w";.

  1. In LocalSettings.php, change $wgArticlePath = "$wgScript?title=$1"; to $wgArticlePath = "/wiki/$1";
  2. Create, or alter, a .htaccess file with the following content in the directory for www.mysite.com

You'll add two lines of code, the first enables the RewriteEngine, and the second tells the server to redirect URLs from /w/index.php?title=Article_name to /wiki/Article_name.

RewriteEngine on
RewriteRule ^wiki/?(.*)$ /w/index.php?title=$1 [L,QSA]

To tell the Apache server to redirect from www.mysite.com/ to www.mysite.com/wiki/Main_Page, you can add:

RewriteRule ^/$ /wiki/Main_Page [R]

Moving MediaWiki to another directory

If you didn't follow the instructions and installed MediaWiki in the directory for www.mysite.com/wiki, then you will need to move it to another directory, such as "w" and then you will need to change $wgScriptPath = "/wiki"; to $wgScriptPath = "/w"; in LocalSettings.php

Troubleshooting

Purging cache

If you notice that your changes to $wgArticlePath in LocalSettings.php are not being reflected in mysite.com/wiki/Main_Page, it may be due MediaWiki's caching of the links according to previous settings.

Go to mysite.com/wiki/Main_Page?action=purge to force MediaWiki to regenerate the cached links.

Also you can:

  1. execute the MySQL query "DELETE FROM objectcache;", or
  2. set $wgCacheEpoch to the current date.

Other issues

If you get internal server errors or similar, check out the Apache error.log. This usually has some clues about the reasons.

See also

See the talk page for more information on setting up non root rewrite rules.

Next page: PHP config >