Manual:Short URL/wiki/Page title -- Apache rewrite--root access

From MediaWiki.org

Jump to: navigation, search

[edit] Apache rewrite

If the Alias method is not suitable (for example, you use PHP as a CGI), you can use Apache mod_rewrite rules instead. mod_rewrite is an Apache module which allows certain request URLs to be changed into other URLs. mod_rewrite is fairly powerful, and also fairly complicated, but for short URLs only a simple rule is required. You can read the full mod_rewrite manual here.

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.

1. First, MediaWiki needs to be configured to generate the correct URL form, "/wiki/Articlename". Add or edit the following setting in LocalSettings.php. If you want to use another path, change /wiki to a different prefix. Although it is possible to use URLs of the form "/Articlename", this can cause problems.
$wgArticlePath = "/wiki/$1";
2. Make sure that the rewrite rule module is installed and accessible. You can check that it is by verifying that you have these lines in their appropriate two locations (among lots of other module lines) in httpd.conf. Without these lines, this method won't work.
# This one is only required if mod_rewrite is built as a DSO instead of
# compiled into Apache.
LoadModule rewrite_module     libexec/apache/mod_rewrite.so
 
AddModule mod_rewrite.c
3. Configure mod_rewrite to rewrite /wiki/Articlename to /w/index.php?title=Articlename. Add the following re-write rules to Apache's httpd.conf. If your MediaWiki installation is inside an Apache virtual host, these should be placed inside the <VirtualHost> stanza.

(The PT option must be used in the RewriteRule to interoperate with an Alias directive being used simultaneously (which is the case e.g. in the Debian MediaWiki package). It doesn't hurt to have PT in other cases, either, save for a small performance hit.)

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

Actually, /w here is the name of the Mediawiki installation directory inside your htdocs. If you have installed your Mediawiki using a different name, replace accordingly. This has to be matched by the line

$wgScriptPath       = "/w";

in your LocalSettings.php.

  • This fix has known problems with httpd-2.0.40-21 on Redhat Linux systems. See the talk page for details.