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.

Example


 * If your wiki is the top dir of your site:

 http://mywiki.site.tld/Article_name 


 * If your MediaWiki is in 'somedir':

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

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.'''

The ampersand problem
A correct edit URL in this scheme would look like:

http://wiki.site.tld/Article_Name?action=edit

'''One of the many problems with this scheme is that you cannot separate edit pages and other dynamic content from the plain page views in robots.txt. This means you either must hide your entire site from search engines, or you will receive much more load from spiders than you would like.'''

After rewriting, we want the URL to look like this:

http://wiki.site.tld/index.php?title=Article_Name&action=edit

There is no need to patch Apache -- using [QSA] on the rewrite rule does this automatically.

Disclaimer
This worked for us on a DreamHost server (don't know the Apache version). It may or may not work for you (but I hope it does... good luck!). Update: The same site was moved to a CentOS (basically RedHat) server running Apache2 and it worked fine on there as well.

The Fix
One successful implementation (according to testing so far...) on the MediaWiki 1.6.6 install at LyricWiki.org used the following .htaccess content to fix the ampersand problem and allow periods in page names. .htaccess RewriteEngine on

RewriteRule ^[^:]*\.(php|src|jpg|jpeg|png|gif|bmp|css|js|inc|phtml|pl|ico|html|shtml)$ - [L,NC] RewriteRule ^index.php?title - [L] RewriteRule ^(.*)\&(.*)$ $1\%26$2 RewriteRule ^(.+)$ /index.php?title=$1 [L,QSA]

Demonstration
http://www.lyricwiki.org/Mind.in.a.box goes to http://www.lyricwiki.org/index.php?title=Mind.in.a.box http://www.lyricwiki.org/Belle_&_Sebastian goes to http://www.lyricwiki.org/index.php?title=Belle_%26_Sebastian

Explanation

 * The first rule stops normal files (as opposed to Wiki pages) from listening to the rewrites. The [L] means that this is the last rule for those files to listen to and kicks them out.  The [NC] means that the rule is Not Case sensitive.
 * The second rule makes sure that pages with index.php?title already in the url don't bother with the rewrite rules.
 * The third rule maps and ampersands to the correct version of the url with %26 substituted in. If this rule were not there, the re-write would not even accept your redirect if you had %26 in the original (pre-re-written) url because it would try to escape the %26 into something else.
 * The last line is the basic rewrite.

Another option
The above solution only handles a single ampersand in the article title. My solution modifies the wiki code instead of htaccess and it extracts the title from the $_SERVER variable. Tested and working on 1.6.7. By Barrylb 19:35, 17 July 2006 (UTC)

Modify includes/WebRequest.php - function WebRequest - put the following after : global $wgArticlePath; if (strpos($_SERVER['SCRIPT_NAME'], 'index.php') === false) { $articlePathPart = str_replace('$1','',$wgArticlePath); $_GET['title'] = $_REQUEST['title'] = str_replace($articlePathPart, '', $_SERVER['SCRIPT_NAME']); }

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 http://www.mediawiki.org/w/index.php?title=Manual:Very_short_URL&action=editusing 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.