Manual:Very short URL

Note:

The recommended guide is located at MediaWiki.org's Manual:Short URL. This is a disorganized collection of various methods and hacked that worked for some users. You should try the recommended guide first.

&larr; Help:System admin

The page describes different ways to make MediaWiki use Short URLs.

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 

Simple httpd.conf or .htaccess method
No patches or kludges required. Two steps:

1. Edit .htaccess or httpd.conf.

and

2. Then add a line to LocalSettings.php.

1) Edit .htaccess or httpd.conf

 * A. Add the following to your .htaccess in your document root directory:

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


 * - Or -


 * B. Edit httpd.conf. First make a note of your DocumentRoot: in this example it is /home/wiki. Then add the following (using your own value of DocumentRoot). Remember to restart Apache after you edit httpd.conf:

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

You can see that the contents of the Directory block are identical to the previous .htaccess. You only need to add this in one place: either .htaccess or httpd.conf, not both. If you have the choice, put it in httpd.conf.

2) Then edit LocalSettings.php
In your LocalSettings.php edit $wgArticlePath as follows:

$wgArticlePath     = "$wgScriptPath/$1";

Note: there may already be a commented out entry for wgArticlePath that looks very similiar to this line; however, it may not be the same as above and may not work for these instructions. Be sure to change the line exactly as shown above.

Second .htaccess method
No patches or kludges required.

This method is the best method if you are not able to change the apache configuration or even do not want it. Within your wiki installation directory change the file .htaccess to the following contents:

# first, enable the processing - Unless your ISP has it enabled # already. That might cause weird errors. RewriteEngine on # test if rewrite should stop for # special directories RewriteRule (images|skins)/ - [L] # all php scripts. RewriteRule \.php$ - [L] # uncomment this rule if you want Apache to redirect from www.mysite.com/ to #  www.mysite.com/Main_Page # RewriteRuin_Page [R] #RewriteRule ^$ /wiki [R,L] # do the rewrite #RewriteRule ^wiki/?(.*)$ /index.php?title=$1 [L,QSA] RewriteRule ^/?(.*)$ /index.php?title=$1 [L,QSA]

Then edit your LocalSettings.php:

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

If you get a Error 500, see Alternate solution with Apache 2.2

Alternate solution 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)

Advantages and Disadvantages to the .htaccess method
Beside the fact that you do not need to edit httpd.conf, Another advantage of this method is that all directories which are not mentioned in the RewriteRule for directories are not reachable from the outside anymore.

The disadvantage of the .htaccess method is that 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.

When using FCKEditor
When using FCKeditor, include on RewriteRule, change: RewriteRule ^(images|skins)/ - [L] to RewriteRule ^(images|skins|fckeditor)/ - [L]

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

This is another approach to setting up MediaWiki for the use of very short URLs.

WARNING: This is a rough sketch, however already tested and seemingly workable.

mod_rewrite and .htaccess support is going to be necessary. This method makes use of an assertion that article titles do not contain dots. This is not very difficult to enforce, and should not pose significant problems.

.htaccess: RewriteEngine on
 * 1) enable the rewriter

RewriteRule \. - [L] RewriteRule ^(.+)$ /index.php?title=$1 [L,QSA]
 * 1) anything that contains a dot is to be left alone.
 * 1) anything else is to be treated as a title

If the dot assertion proves difficult, one can try to use a different set of rules, allowing typical files to be accessed directly, while treating everything else as article names. Again, however, this prevents naming articles after files.

RewriteRule \.(php|html|gif|jpg|png|css|js)$ - [L] RewriteRule ^(.+)$ /index.php?title=$1 [L,QSA]
 * 1) anything that ends in a known file extension is to be left alone.
 * 1) anything else is to be treated as a title

Slight tweak that seems to work best (so /Image:Foo.jpg and /images/a/bc/Foo.jpg both work):

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

Another tweak so files outside the wiki directory will work 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
 * 1) anything that contains a slash is to be left alone.

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  

complete redirection
As long as you are rewriting the 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 /wiki/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.

A more complete way would be to not use the installation directory in the URL. The directory in the URL can contain only a .htaccess file that rewrites into the real location. For example, say your wiki is in /mediawiki-1.7.1/, and you want the URLs rewritten like /wiki/Main_Page.

Then, you create the /wiki directory, with only the wiki/.htaccess file with the rewrite rule:

Options FollowSymLinks RewriteEngine on RewriteRule ^(.*) /mediawiki-1.7.1/index.php/$1 [QSA]

Then, in mediawiki-1.7.1/LocalSettings.php, add the line

$wgArticlePath = "/wiki/$1";

Now, the real URLs are all like /mediawiki-1.7.1/index.php/Main_Page, the rewritten ones are under /wiki/Main_Page, and you can name article whatever you want; even /wiki/index.php would be in article space.

older method
Due to problems in Apache's pattern matching, you either have to patch Apache or perform a kludge.

This page is current as of MediaWiki version 1.4.5.

You could also try another approach, needing no Apache patches nor MediaWiki modifications.

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</tt> 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</tt> 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 ) ) {

<p class='error'>'''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</tt>, 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 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.

MediaWiki 1.82 .htaccess method for wikis stored in subfolder of root
I have gotten Mediawiki Pretty URLs to work here at by using this configuration. I have my wiki in the wiki5 folder. I'm running, MediaWiki 1.82

Add to .htaccess

AddHandler application/x-httpd-php5 .php

The above line directs all php files to processed using PHP5

RewriteCond %{HTTP_HOST} ^muscatelli.info/wiki5 RewriteRule ^(.*)$ http://www.muscatelli.info/wiki5/$1 [r=301,L]
 * 1) Redirect http://muscatelli.info to http://www.muscatelli.info

Note: the above is used to redirect all addresses with the www removed to [www.muscatelli.info] to help Google Ranking and is not necessary for the code to work! Also note that my wiki is in the directory: wiki5

RewriteRule ^[^:]*\. - [L] RewriteRule ^[^:]*\/ - [L] RewriteRule ^/*$ /wiki5/index.php?title=Main_Page [L,QSA] RewriteRule ^(.+)$ /wiki5/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
 * 1) anything that contains a slash is to be left alone.

Change in LocalSettings:

$wgArticlePath     = "$wgScriptPath/$1";

Confirmed to work in 1.6.10. ~Arden