Manual:Short URL/wiki.example.com/Page title--Subdomain using mod-rewrite

From MediaWiki.org

Jump to: navigation, search

[edit] Subdomain with no Subdirectory in Article URL using mod-rewrite

Warning: this method may create an unstable URL structure and leave some page names unusable on your wiki. See Manual:Wiki in site root directory.

Please see the article Cool URIs don't change and take a few minutes to devise a stable URL structure for your web site before hopping willy-nilly into rewrites into the URL root.

Apache's mod-rewrite can be used to allow the shortest URL's of just http://yourdomain/pagename. Mod-rewrite changes the illegally formatted "friendly" URL request into a legitimate request directed at the index.php script like usual. To use mod-rewrite, specify a condition pattern, and a transformation rule to apply to the URL if it matches the condition pattern, such as in the following example:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/w.*/
    RewriteRule ^(.*) /wiki/index.php/$1 [L]

This re-write condition says not to touch any URL's which start with a lowercase W and start and finish with a forward slash, this protects all old-style article requests, and requests to internal MediaWiki files like scripts or images. By not specifically using the term wiki in the condition allows other directories of web documents outside the wiki such as /www/media

The re-write rule then assumes everything after the leading slash to be an article name and reformats the URL accordingly. Also change LocalSettings.php to set

$wgArticlePath = "/$1"

This is necessary to make the friendly form the default way of rendering links. Links involving a query-string will use the usual format.

This uses the technique for evading the Manual:Short URL#Ampersands and other encoded characters in titles problem with ampersands described below.

Personal tools