Short URL

Aus IV1

According to World Wide Web inventor Tim Berners-Lee, good page addresses should never change[1]. Short URLs which hide complex programming code from the page address is good for webpage visitors. Please take a few minutes to devise a stable URL structure for your website before getting started, to reduce problems later.[2] This page has been divided into separate "how to" mini-guides listed below to make things easier.

Introduction

Defaults

MediaWiki's default installation path usually looks something like this:

/var/www/localhost/htdocs/mediawiki/w (installed as root user)
/home/johndoe/public_html/mediawiki/w (installed with a shared hosting provider)

MediaWiki's default page addresses looks like these examples:

http://example.com/w/index.php/Page_title (recent versions of MediaWiki, unless using CGI)
http://example.com/w/index.php?title=Page_title (recent versions of MediaWiki, using CGI)

Using the methods below, short webpage addresses can be changed to addresses such as these:

http://example.com/wiki/Page_title (this is the standard, same as in Wikipedia)
http://wiki.example.com/Page_title (not recommended!)

Advantages and disadvantages

Long URLs have a key advantage: they work in all hosting environments — a good thing to know lest one day you move your wiki to a hosting environment where you can't keep using the same URLs everyone has bookmarked or linked to.

The advantages of short URLs are that they hide all technical details, as is best practice for URLs [1] -- among other things this means they can be kept stable when the underlying mechanism change, if your hosting environment supports this. They are also easier for visitors to remember and allow visitors to navigate the site by URL more conveniently.

Once you've decided that you really want short URLs and are willing to go through the effort, you're ready for the long trial and error configuration as detailed below.

Shared hosting

Most shared hosting systems do not allow changes to httpd.conf. If you are using a shared host, first try asking your hosting provider who may well solve your problem for you. If that doesn't work out, depending on your hosting provider, you may be able to edit .htaccess (detailed below).

If you have a choice, you should edit httpd.conf (which requires root access and is the preferred method because your wiki will perform better[3]). You only need to edit either .htaccess or httpd.conf, not both.

Recommended how-to guide (setup used on Wikipedia)

The following setup is used on Wikipedia, and tends to be robust and easy to set up. This setup should be used unless you really dislike some aspects of it (it doesn't rewrite all URLs) or you simply can't use it (it requires root access).

Advantages

  • This method is reliable and guaranteed to work in all versions of MediaWiki for the indefinite future. Other schemes are not tested by MediaWiki developers and might break when changes are made to the software.
  • Not all URLs are rewritten, but this is good. With a single simple rule, this allows you to easily block search engine spiders from viewing background pages (like the edit form or history pages), separate actual article views from other accesses in log analysis, et cetera. Although links to edit pages and background pages are slightly less memorable, this is no big deal: the important thing is the articles.
  • The method uses Alias instead of Rewrite. This is simpler and more reliable.

Disadvantages

  • This method requires Apache and root access.

Setup steps

First of all, ensure that you are using Apache and have root access (so you can modify httpd.conf; modifying .htaccess is not enough!). If either of these is false, you cannot use this exact method; you need to modify it appropriately, or use some other method listed below, preferably Manual:Short URL/wiki/Page title -- no root access.

  1. Choose a virtual directory in which you want your articles to appear. This guide will assume that you choose /wiki/ (as Wikipedia does), articles will then be accessed like http://www.example.com/wiki/Article_title. Do not create this virtual directory and it should not exist in the web root folder!. This guide assumes that you have installed MediaWiki in /w/ folder relative to your web root as Wikipedia does, and not in /wiki/ folder.
    Vorlage:NoteIn case you earlier chose /wiki/ as your folder for installing MediaWiki unknowingly, then rename the folder name to new name (here /w/) and replace /wiki/ with new path (here /w/) wherever you see it (one typically only need to change LocalSettings.php file).
    Vorlage:NoteThe install path must not be same as virtual directory and must not be the web root directory itself, the first point is important, do not try to ignore it to get prettier URLs, or else this method will not work.
  2. At the bottom of LocalSettings.php, add the following:
    $wgScriptPath = "/w";         # Path to the actual files. This should already be there
    $wgArticlePath = "/wiki/$1";  # Virtual path. This directory MUST be different from the one used in $wgScriptPath
    $wgUsePathInfo = true;        # Enable use of pretty URLs
    
    Note that $wgScriptPath should already be set to /w. If it is not (for example, if you're moving from a different URL scheme), make sure to set it to that.
  3. Edit httpd.conf to contain the rule Alias /wiki /path/to/your/wiki/index.php. Note that the second path is relative to the filesystem root, not the web root! This might be, for instance, Alias /wiki /var/www/w/index.php, if /var/www/ is your web root. This ALIAS configuration is a minimum configuration, see Apache configuration for further options.
  4. Make Apache reread its configuration file (gracefully restart). You might use, for instance, apachectl graceful or a similar command as root, or use features of WebHost Manager or similar software.
  5. In your web root, create a file called robots.txt if one does not already exist. Then add the following to the end of the file:
    User-agent: *
    Disallow: /w/
    Disallow: /wiki/Special:Search
    Disallow: /wiki/Special:Random
    
    The first "Disallow" rule stops spiders from indexing histories, edit pages, and other background pages that are useless to users performing a search. This will also prevent duplicate content from being indexed. The second and third rules stop spiders from indexing two special pages that might confuse them, and generally be unhelpful.

You're done; your wiki should be working perfectly. If not, go to irc://irc.freenode.org/mediawiki and report any problems.

Other how-to mini-guides

Anyone is welcome to create a how-to solution page and list it below. Please use a sensible name for the page, one that fits in with the below names. When each unique solution has its own page, readers can skip complexity they do not want. Keep it simple, readable, short, with a separate page per separate solution.

To help others find out which Short URL methods really work, after trying each method please edit the page and increase the "worked" or "didn't_work" numbers for that guide and make a brief (or long, your choice) description on what went wrong by clicking the link on your number.

URL like - example.com/wiki/Page_title

How to create example.com/wiki/Page_title URLs: Vorlage:/guide - recommended method if you don't have root access; should also work with PHP as CGI mode Vorlage:/guide Vorlage:/guide Vorlage:/guide Vorlage:/guide Vorlage:/guide Vorlage:/guide 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 the "no root access" examples instead.

Vorlage:/guide Vorlage:/guide Vorlage:/guide (If the Alias method is not suitable [for example, you use PHP as a CGI], you can use Apache instead.) Vorlage:/guide Vorlage:/guide

URL like - example.com/Page_title

Vorlage:Wiki-in-docroot

How to create example.com/Page_title URLs: Vorlage:/guide Vorlage:/guide Vorlage:/guide (used this method: Datei:Face-sad.svg x2) Vorlage:/guide (also works with 1.10 & 1.13, but not with 1.11., very similar to the above solution)

URL like - example.com/wiki.php/Page_Title

Vorlage:/guide

URL like - example.com/sub_directory/wiki/Page_Title

Vorlage:/guide

URL like - wiki.example.com/Page_title

Vorlage:Wiki-in-docroot

How to create wiki.example.com/Page_title URLs. Vorlage:/guide Vorlage:/guide Vorlage:/guide Vorlage:/guide Vorlage:/guide

Troubleshooting

Ampersand (&) problem

The ampersand problem shows up when you have page titles with symbols in (such as &, ?, #, + and /) that, despite being correctly encoded in the link are not being passed correctly from mod_rewrite to the script. This manifests in 404 page-not-found errors, because the title gets cut off at the special character. For example, clicking on a link to "John & Maria's page" gets a 404, because MediaWiki is looking for a page named "John ".

This is because ampersands in long-form names are treated as query string separators, and would never reach the PHP runtime environment. This is caused by an old and problematic mod_rewrite bug. There are discussions of other possible solutions at lists.wikimedia.org and fgiasson.com.

Solutions: Vorlage:/guide Vorlage:/guide

Looping alias/rewrite errors

If you receive looping alias/rewrite errors such as "Cannot find page www.example.com/wiki/wiki/wiki/wiki/wiki/ [...] /index.php", try one of these fixes:

Vorlage:/guide Vorlage:/guide

Revert to default

If you need to revert to the default values of your wiki but have accidentally deleted them, here are the defaults:

$wgScriptPath       = "/wiki";
$wgScript           = "$wgScriptPath/index.php";
$wgRedirectScript   = "$wgScriptPath/redirect.php";

## For more information on customizing the URLs please see:
## http://meta.wikimedia.org/wiki/Eliminating_index.php_from_the_url
## If using PHP as a CGI module, the ?title= style usually must be used.
# $wgArticlePath      = "$wgScript=$1";
 
$wgArticlePath      = "$wgScript?title=$1";

Tricks

Vorlage:/guide Vorlage:/guide Vorlage:/guide Vorlage:/guide

See also

External links

References

  1. Cool URIs don't change by Tim Berners-Lee. (Interestingly, the short URLs discussed on this page are the ones that will break if one day one moves to a system where only the long URLs work!)
  2. Avoid referring to locations in the same directory. Doing so can create redirection loops that are hard to get rid of and will always leave holes in the redirection. (One might argue that long URLs are the most "stable" ones, being immune to hosting company environment changes, etc.)
  3. Should you edit httpd.conf or .htaccess? While .htaccess does not require root access, there is a performance penalty as httpd then has to search up the directory tree and process all .htaccess files which may apply. See the Apache article, When (not) to use .htaccess files.

Vorlage:Languages