Manual:Short URL
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 are 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]
Contents |
[edit] Introduction
[edit] 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!)
[edit] Advantages & disadvantages
Long URLs are said to have one minor advantage; they work in really bad hosting environments that don't support any sort of rewrite or alias at all. However every widely used webserver is capable of being configured to use short urls, practically every paid web host should support this feature, and even most of the free hosts (which we don't recommend the use of) support short urls. To the point where any host that doesn't offer you this feature is not worth your money.
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. They are also easier for visitors to read and remember. And they allow visitors to navigate the site by URL more conveniently.
[edit]
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.
[edit] 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).
[edit] 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.
[edit] Disadvantages
- This method requires Apache and root access.
[edit] 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/Apache.
- Find the directory in which your wiki was installed. By default, this is a subdirectory called /w, within your web server's document root (web root). The web root is the directory where you put all the html files that your web server makes publicly available. For instance, if your web root is /var/www, then your wiki is probably in /var/www/w. Inside this directory you should see files like index.php and LocalSettings.php. This can all be a little confusing, because on some systems (e.g., Debian), the subdirectory /w may actually be a symbolic link (e.g., to /usr/share/mediawiki/).
- 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.
Note: In 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 needs to change wgScriptPath in LocalSettings.phpfile).
Note: The 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. - In LocalSettings.php, modify with the following:
$wgScriptPath = '/w'; # Path to the actual files.
$wgArticlePath = '/wiki/$1'; # Virtual path. This directory MUST be different from the one used in $wgScriptPath
Note that$wgUsePathInfo = true; # Enable use of pretty URLs
$wgScriptPathshould 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. - Edit httpd.conf to contain the rule
Alias /wiki /path/to/your/wiki/index.php.- This is pointing to the file index.php, not a directory!
- 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. If this does not work or you get a 403 error, use an absolute path that begins with the drive MediaWiki is installed on such asC:/absolute/path/to/your/wiki/index.php. This ALIAS configuration is a minimum configuration, see Apache configuration for further options. If you're using Apache 2, the configuration file is called apache2.conf. If you don't have a httpd.conf, then use this.
- Make Apache reread its configuration file (gracefully restart). You might use, for instance,
apachectl gracefulor a similar command as root, or use features of WebHost Manager or similar software.If you're using Apache 2, the command is apache2ctl graceful. - Add
AcceptPathInfo onto your Apache config if it doesn't have yet. - 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
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.Disallow: /wiki/Special:Random
Note: If you're using a localized version of MediaWiki (non-English) the names of the Special pages will be translated. Check the actual links in your wiki for the special page URLs. These are independent of the personal language choice of individual users.
You're done; your wiki should be working perfectly. If not, go to irc://irc.freenode.org/mediawiki and report any problems.
[edit] Guides
Our official guides for short URL configuration for different web servers. Pick your webserver's configuration from this list.
- Apache - How to configure short URLs on Apache. (Most shared hosts use Apache, so if you don't know what web
[edit] Other how-to mini-guides
- These guides are old and are almost entirely bad advice. These will eventually be deleted one by one as our official guides above are created for different webservers.
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.
[edit] URL like - example.com/wiki/Page_title
[edit] 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.
- wiki/Page title
x19
x2 - wiki/Page title -- Lighttpd rewrite--root access
x2 - wiki/Page title -- nginx rewrite--root access
x1 - wiki/Page title -- Cherokee rewrite--root access
x1 - wiki/Page title -- Windows with IIS7--root access
x1
x1
[edit] URL like - example.com/Page_title
|
|
Warning: this method may create an unstable URL structure and leave some page names unusable on your wiki. See Manual:Wiki in site root directory. 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. |
How to create example.com/Page_title URLs:
- Page title -- Solution for mediawiki-1.12
x4
x1 (also works with 1.10 & 1.13, but not with 1.11., very similar to the above solution) - Page title - nginx, Root Access, PHP as a CGI module
x4 - Page title -- Windows & Apache without 403 on Special Pages
x1
[edit] URL like - example.com/sub_directory/wiki/Page_Title
[edit] URL like - wiki.example.com/Page_title
|
|
Warning: this method may create an unstable URL structure and leave some page names unusable on your wiki. See Manual:Wiki in site root directory. 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. |
How to create wiki.example.com/Page_title URLs.
- wiki.example.com/Page_title--Subdomain using mod-rewrite
x4
x1 - wiki.example.com/Page_title--Subdomain using Lighttpd
[edit] Troubleshooting
[edit] No Skins
With Ubuntu 8.10 and Mediawiki 1.11.2 (and maybe later versions) if you use the Mediawiki sanctioned directions your skins may fail to load. To fix this, create a symbolic link in /var/www pointing to /var/lib/mediawiki with the value of $wgScriptPath in LocalSettings.php. If, for example, $wgScriptPath is set to "/w" then you need to run the command "sudo ln -s /var/lib/mediawiki /var/www/w".
Alternatively, try setting $wgLoadScript to the original file. If you source url is /wiki with the short url /w, set it to /wiki/load.php.
[edit] Purging cache
If you notice that your changes to $wgArticlePath in LocalSettings.php are not being reflected in mysite.com/wiki/Main_Page, it may be due MediaWiki's caching of the links according to previous settings.
Go to mysite.com/wiki/Main_Page?action=purge to force MediaWiki to regenerate the cached links.
Also you can:
- execute the MySQL query "TRUNCATE objectcache;", or
- Ensure $wgCacheEpoch is updated (if $wgInvalidateCacheOnLocalSettingsChange is enabled all you have to do is edit LocalSettings.php to purge it)
[edit] Redirect visitor of your domain to your wiki
Drop a index.php file in your web root directory with the following lines:
<?php header( 'Location: http://yourdomain/w/' ) ; # header of web site index is located elsewhere.
Another option is to use a redirect in your Apache config file if you have mod_alias enabled:
RedirectMatch ^/$ /wiki
[edit] Tricks
[edit] See also
- Explanation of what CGI modules are
- $wgUsePathInfo - configuration setting that defines whether to use 'pretty' URLs or not
- Notes on placing your wiki installation in site root directory
[edit] External links
- Apache Module mod_rewrite at Apache.org
- MediaWiki Short URL Builder Tool - A tool that can automatically generate short url configuration more reliable than the configurations in these manual pages.
[edit] References
- ↑ 1.0 1.1 Cool URIs don't change by Tim Berners-Lee.
- ↑ 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.
- ↑ 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.
| Language: | English • 日本語 • Русский • 中文(简体) |
|---|