Manual:Very short URL

Note: This page is a disorganized collection describing various methods and hacks to make MediaWiki use very short URLs, that worked for some users. Before trying any of these methods, you should first read the recommended guide located at Manual:Short URL.

Apache Rewrite method
Besides not requiring access to edit httpd.conf, the advantage of editing .htaccess is that all directories which are not mentioned in the RewriteRule for directories are not reachable from the outside anymore. However there is a performance penalty, as httpd then has to search up the directory tree and process all .htaccess files which may apply. See When (not) to use .htaccess files. You only need to edit either .htaccess or httpd.conf, not both. If you have the choice, edit httpd.conf.

Basics
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 for the URL root. As long as you are rewriting URLs to another location in the same directory, there will always be holes in the redirection; article names that can't redirect, because they have to refer to local files. For example, if /index.php were to redirect, you would get a redirect loop; by preventing that, then an article called index.php wouldn't use the same URL.

Taking note of your DocumentRoot. Which is /home/wiki in this example. Add to /home/wiki/.htaccess or to httpd.conf:

As you can see, the contents of the Directory block are identical to .htaccess.

Adding Exceptions
Some exceptions are needed to prevent looping and to make things work as they should:

For example: RewriteEngine On RewriteRule ^(images/skins)/ - [L] # so skins and images work RewriteRule ^/*$ /index.php?title=Main_Page [L,QSA] RewriteRule ^(.+)$ /index.php?title=$1 [PT,L,QSA]

Remember after you edit httpd.conf to restart Apache.

Edit LocalSettings.php
Edit your LocalSettings.php as follows:

$wgScriptPath      = ""; $wgScript          = "$wgScriptPath/index.php"; $wgRedirectScript  = "$wgScriptPath/redirect.php";  # # Theoretically the only line you should have to edit $wgArticlePath     = "/$1";

Alternatively with Apache 2.2 edit LocalSettings.php as follows:

$wgScriptPath      = ""; $wgScript          = "/index.php"; $wgRedirectScript  = "/redirect.php"; # Theoretically the only line you should have to edit $wgArticlePath     = "/$1";

Or:

$wgArticlePath     = "$wgScript/$1"; (In 1.6.8 MediaWiki version)

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

Aliasing method
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.

This modification will make very short url, but return Log in not correctly redirected back and Log out not working (unless you repair the URL by hand) due to improper script handling, as well as wrong handling of page that wasn't edited before.

If you find a solution please add it in here.

Edit LocalSettings.php variables:

$wgScriptPath      = "/mediawiki1.8"; $wgScript          = "/index.php"; $wgArticlePath     = "/$1";

Edit Apache2 configuration file (require mod_alias enabled):

 Alias /mediawiki1.8 /var/lib/mediawiki1.8/ Alias /index.php /var/lib/mediawiki1.8/index.php/ Alias / /var/lib/mediawiki1.8/index.php/

 Options -Indexes  

Warnings and disclaimer
'''This configuration is totally unsupported by the Mediawiki development team. Therefore, future versions may have modifications that break this setup. Indeed, Mediawiki's current code must be modified in two places before it can work.'''

'''Also, it displaces the location for wiki pages. Allowing users to load non-wiki pages in the same webpage will be difficult to set up.'''

What configuration??? The above or below! Fix this header please.

Patching Apache
A few Apache 2 packages are built with this patch already applied (e.g., Gentoo's). Otherwise:

Get the Apache 2 source. Download the patch. Apply, compile, install.

httpd.conf
In httpd.conf or your vhost-config, you'll need to enable some RewriteRules for the wiki's website. Add the following in the appropriate place (e.g., in a VirtualHost):

RewriteEngine On RewriteCond %{REQUEST_URI} !^/(stylesheets|skins|images|config)/ RewriteCond %{REQUEST_URI} !^/(index|redirect|texvc)\.php$ RewriteCond %{REQUEST_URI} !^/error/(40(1|3|4)|500)\.html$ RewriteCond %{REQUEST_URI} !^/(favicon\.ico|robots\.txt)$ RewriteMap ampescape int:ampescape RewriteRule ^/(.*)$ /index.php?title=${ampescape:$1} [L,QSA]
 * 1) Don't rewrite requests for files in MediaWiki subdirectories,
 * 2) MediaWiki PHP files, HTTP error documents, favicon.ico, or robots.txt

Note that the line that prevents rewriting requests for error documents may vary from site to site. Note that you can't use that in .htaccess - RewriteMap isn't valid in htaccess-Context!

MediaWiki
Function getLocalURL in includes/Title.php needs to be modified as follows. As of 1.5.5 this was line 781:

- $url = "{$wgScript}?title={$dbkey}&{$query}"; + $url = "/{$dbkey}?{$query}";

'''As noted above, this will destroy the ability to separate dynamic requests from page views in robots.txt and will create useless extra traffic for your server. It is highly recommended that you do not do this.'''

Function view in includes/RawPage.php needs to be modified as follows. As of 1.5.5 this was line 73:

- if( strcmp( $wgScript, $url ) ) { + if( 0 and strcmp( $wgScript, $url ) ) {

'''This disables protection against a cross-site scripting vulnerability for Internet Explorer clients. DO NOT DO THIS if you want to cater to broken software products!'''

In LocalSettings.php, make sure these variables are set as follows:

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

Fin.

'''$wgScript should always be set to an actual URL path to the script. Making it empty may create invalid form actions and other URLs, and may break on some browsers.'''

Kludge
If you aren't going to use a patched Apache server, do the following:

$url = "/{$dbkey}&{$query}";
 * Apply the changes to MediaWiki as instructed in the previous section, except that the line in includes/Title.php</tt> has an ampersand instead of a question mark:

RewriteRule ^/(.*)$ /index.php?title=$1 [L]
 * Apply the changes to httpd.conf</tt> as instructed above, except:
 * The RewriteMap</tt> line is omitted;
 * The final RewriteRule</tt> line is:

This solution generates URLs that contain ampersands where the question marks should be. The RewriteRule will insert the question mark in the proper place.

In practice, it works fairly well. Smart URLs, such as those used in Firefox, will need to have an ampersand where the question mark would normally be. There may be other situations in which it could cause problems.

'''Don't do this either -- it will create a large number of broken links if you ever change back to a correct configuration. It's also completely unnecessary, as the problem it's meant to solve doesn't require patching Apache. Use the [QSA] option on the rewrite rule. (The ampescape patch is for a different issue, which as far as I can see this would not help with.)'''

Making it work with robots.txt
It is also possible to make regular pages appear shortened like this but other pages will appear in full. This could be done a number of ways, but the most common are to make edit links like http://wiki.site.tld/edit.php?title=Article_Name or http://wiki.site.tld/edit/Article_Name. The first one should be obvious how to implement and the second is just as easy as using a single rewrite rule. This would also have to be done for any other features which you wish to hide from robots, for instance history.

Method for Debian
DON'T install the mediawiki package. It installs to /var/lib/mediawiki1.7 (On stable at least), and makes an alias. This interferes with all the redirects. instead, install from source to /var/www/mediawiki. Then run chmod a+wx /var/www/mediawiki/config then install as you would the package ( http://localhost/mediawiki/config, follow instructions.)

Then run a2enmod rewrite. Edit your virtualhosts file and add the folowing:

RewriteEngine on RewriteCond %{REQUEST_URI} !^/favicon.ico RewriteCond %{REQUEST_URI} !^/robots.txt RewriteCond %{REQUEST_URI} !^/mediawiki/ RewriteRule ^/(.*)$ /mediawiki/index.php/$1 [L,QSA]
 * 1) Don't rewrite requests for files that really exist or should return 404.
 * 1) Rewrite http://wiki/article -- this is the main rule

Then add this to /var/www/mediawiki/LocalSettings.php

$wgArticlePath = "/$1";

Run apache2ctl graceful one more time, and you should be done.