Manual:Short URL

Short URLs that hide ugly PHP mechanics from the browser location bar for normal pages are good user interface. They are not implemented in MediaWiki by default however because they are done in concert with Web server configurations that can get complicated and may vary depending on the specific Web server being used.

In all the examples, it is assumed that MediaWiki is installed in the directory located at  and that you want to use   as an alias for. It is possible to use an alternative install directory and an alternative alias simply by changing these values ("wiki" and "w") in the examples below. The process is slightly more complicated if you want both to be the same, or if you want the alias to be the root directory ("" instead of "wiki"), but in both cases it is possible with a bit of tweaking (and a few usage restrictions).

No root access
These methods assume that you don't have access to the server configuration (for example, if you're on a shared host); if you do, see 'Root access' below. You can use an alias or a rewrite, but preferably not both.

Alias
You can set an alias in .htaccess, so that the server will invisibly replace "wiki" with "w/index.php".


 * Alias is not allowed in .htaccess files, at least on Apache 2.0, or did I miss something? See the Alias Directive documentation.

Solution without Alias
I struggled for a few hours have have found this solution. My site (http://www.eroswiki.com) is in ~/eroswiki/wiki. Using this .htaccess in my ~/eroswiki directory, I was able to use short URLs and the redirection I wanted. My site is hosted at 1 & 1.

~/web_root/.htaccess
RewriteEngine On RewriteCond %{HTTP_HOST} ^domain.com RewriteRule ^(.*)$ http://www.domain.com/$1 [r=301,L] RedirectMatch ^/$ http://www.domain.com/wiki RewriteCond %{REQUEST_URI} ^/wiki$ RewriteRule ^(.*) /wiki/index.php?title=Main_Page [L]
 * 1) Redirect http://domain.com to http://www.domain.com

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

~/web_root/wiki/LocalSettings.php
$wgServer = "http://www.domain.com"; $wgArticlePath = "/wiki/$1";

Rewrite
You can rewrite the URL, so the server will invisibly load "w/index.php?title=article" for "wiki/article".

Root access
These methods require that you have access to the server configuration. If you are on a shared host, you most likely don't; see 'No root access' above.

Alias directive
If you use Apache httpd with mod_php (not CGI or FastCGI PHP), the easiest way to have short URLs is with an Alias rule.

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.

Subdomain
You can make MediaWiki use a subdomain like "wiki.example.com/foo". This assumes that you already have a subdomain ready; if not, set one up or contact your host.


 * 1) Use one of the methods above to reduce "/wiki/index.php?title=foo" to "/wiki/foo".
 * 2) Add $wgServer to LocalSettings.php with your subdomain. $wgServer = "http://subdomain.example.com";
 * 3) Blank the value of $wgScriptPath in LocalSettings.php, so that MediaWiki links to the domain. $wgScriptPath = "";

Subdomain with no Subdirectory in Article URL
Warning: You should never do this as it creates an unstable URL structure, requiring a never-ending series of special-cases leading to various page names being unusable on your wiki.

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.

If you want to use something like http://wiki.example.com/Article_Title, make the following changes to .htaccess and LocalSettings.php after installing. These instructions assume MediaWiki is installed to wiki/, the subdomain is only for the wiki, and that the main page is Main_Page. .htaccess</tt> lines are pretty much exactly what the owner of http://www.muscatelli.info/ posted on the discussion page.

.htaccess
The first step is to make sure that requests for /Article_Title get interpreted correctly. The following lines should be added to .htaccess in the root of the wiki.example.com domain:

RewriteEngine On RewriteRule ^[^:]*\. - [L] RewriteRule ^[^:]*\/ - [L] RewriteRule ^/*$ /wiki/index.php?title=Main_Page [L,QSA] RewriteRule ^(.+)$ /wiki/index.php?title=$1 [L,QSA]
 * 1) anything that contains a dot without a colon should be left alone
 * 1) anything that contains a slash without a colon should be left alone
 * 1) redirect to main page
 * 1) anything else is to be treated as a title

LocalSettings.php
Now that URLs like http://wiki.example.com/Article_Title work, all that remains is to tell MediaWiki to use those URLs. This is done by defining $wgArticlePath</tt> in LocalSettings.php</tt>. Here's an example, defined just after $wgScriptPath</tt>:

$wgScriptPath      = "/wiki"; $wgArticlePath     = "/$1";

Subdomain with no Subdirectory in Article URL using mod-rewrite
Warning: You should never do this as it creates an unstable URL structure, requiring a never-ending series of special-cases leading to various page names being unusable on your wiki.

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 $wgArticlePath = "/$1"</tt> is set in LocalSettings.php to make the friendly form the default way of rendering links. Links involving a query-string will use the usual format.

Note that I'm transforming the reqested URL to the semi-friendly format of .../wiki/index.php/pagename</tt> rather than to the long format of ...index.php?title=pagename</tt>. The reason for this is that using the long form means that articles can't have ampersands in their name because they would be treated as query string separators and would never reach the PHP runtime environment. The semi-friendly format is descriptive enough for apache to determine that the index.php script should handle the request. MediaWiki is prepared for this format and obtains the article title directly from the full request information in the $_SERVER</tt> hash.
 * Ampersands in titles

Subdomain with no Subdirectory in Article URL using Lighttpd
To put mediawiki in a root directory with the Lighttpd webserver, use the following rule (assuming mod_rewrite is loaded):

url.rewrite-once = (   "(^/[^:]*[\./].*)" => "$1",    "^/([^?/]*)(?:\?(.*))?" => "/wiki/index.php?title=$1&$2", )

If you want to host this on a seperate domain (which is very likely for a wiki in the root-dir) do:

$HTTP["host"] == "wiki.example.com" { server.document-root = "/path/to/webroot" url.rewrite-once = (       "(^/[^:]*[\./].*)" => "$1",        "^/([^?/]*)(?:\?(.*))?" => "/wiki/index.php?title=$1&$2",    ) }

If you want to allow ampersands in titles too (or any other character that might be considered an argument delimiter in the query string) you can make a more complicated rule using mod_magnet. For more information, see lighttpd's documentation about mod_magnet on their website.

Prevent bots from crawling index.php
You can make sure that search engines only index actual wiki pages, without indexing action views (such as edit or history pages, with URLs in the form ). Note that doing this without implementing short URLs will block all pages from being indexed.

Create a file named robots.txt</tt> in the root of your MediaWiki installation with the following content. User-agent: * Disallow: /index.php