From mediawiki.org
Jump to navigation Jump to search
This page is a translated version of the page Manual:Short URL and the translation is 44% complete.
Other languages:
Deutsch • ‎English • ‎español • ‎français • ‎italiano • ‎português • ‎português do Brasil • ‎svenska • ‎русский • ‎中文 • ‎日本語 • ‎한국어

缩短的URLURL重写会使页面地址中隐藏php文件扩展名。 在开始之前,请花几分钟时间为您的网站设计一个“稳定的URL结构”,以减少问题。




/var/www/localhost/htdocs/mediawiki/w (用Root账户安装)
/home/johndoe/public_html/mediawiki/w (虚拟主机安装)


http://example.org/w/index.php/Page_title (Mediawiki的最新版本,无CGI支持)
http://example.org/w/index.php?title=Page_title (Mediawiki的最新版本,有CGI支持)

使用下面将提到的方法, 我们可以把地址设置成下面的样子:

http://example.org/wiki/Page_title 这是最常见的配置,维基百科也采用同样的配置,但不是缺省值,因为它需要配置服务器。


  • 优势:缩短URL会对终端用户隐藏大部分技术细节,这对大部分站点拥有者都是有用的。除此之外,这还意味着当底层软件变动时可以保持url的稳定性。 (请参阅上文,了解MediaWiki的默认URL结构与CGI启用和不启用之间的区别)。 对访问者来说,他们也更易于记忆并且让访问者更方便的根据URL进行导航。 此外,短URL提供优于搜索引擎优化的默认URL的优势,因为在计算页面排名时,搜索引擎可以不利地处理包含?的URL。
  • 劣势:需要配置,因此,根据您使用的托管环境,可能很难重新创建。 但是,由于每个广泛使用的Web服务器都能够配置为使用短URL,因此大多数付费Web托管服务都应支持此功能。任何不提供此功能的主机都不值得你花钱。


我们为不同Web服务器配置短URL的官方指南。 从此列表中选择您的Web服务器配置。 如果你不知道你正在使用什么服务器软件,那么它很可能是Apache。


这些指南很旧,几乎是完全不好的建议。 这些将最终逐个删除,因为上面的官方指南是为不同的Web服务器创建的。

欢迎任何人创建一个操作方法解决方案页面并在下面列出。 请为页面使用合理的名称,该名称适合以下名称。 当每个独特的解决方案都有自己的页面时,读者可以跳过他们不想要的复杂性。 保持简单、可读、简短,每个单独的解决方案都有一个单独的页面。

为了帮助其他人找出真正有效的短网址方法,在尝试每种方法后,请编辑该页面并增加该指南的“工作”或“未工作”数字,并对内容做出简短(或长期,您的选择)描述 单击您的号码上的链接出错了。


Actions such as edit, view history, etc. will still have index.php unless $wgActionPaths are defined.


These methods require that you have access to the server configuration. If you are on a shared host, you most likely don't. In this case you will be required to use a method that requires "no root access". You can jump directly to the MediaWiki ShortURL Builder tool and follow the on-screen instructions. Select "I don't have root access" after entering your wiki's URL.

  • [[Special:MyLanguage/Manual:Short URL/wiki/Page title -- Lighttpd rewrite--root access|wiki/Page title -- Lighttpd rewrite--root access]] Face-smile.svgx3
  • [[Special:MyLanguage/Manual:Short URL/wiki/Page title -- nginx rewrite--root access|wiki/Page title -- nginx rewrite--root access]] Face-smile.svgx3
  • [[Special:MyLanguage/Manual:Short URL/wiki/Page title -- Cherokee rewrite--root access|wiki/Page title -- Cherokee rewrite--root access]] Face-smile.svgx1
  • IIS7
  • [[Special:MyLanguage/Manual:Short URL/wiki/Page title -- Windows with IIS7--root access|wiki/Page title -- Windows with IIS7--root access]] Face-smile.svgx2 Face-sad.svg[[Manual talk:Short URL/wiki/Page title -- Windows with IIS7--root access/Problem|x3]]
  • IIS8.5 Face-smile.svgx1 Face-sad.svgx1
  • Abyss
  • [[Special:MyLanguage/Manual:Short URL/wiki/Page title -- Saetta Web Server--root access|wiki/Page title -- Saetta Web Server--root access]]

(for Apache guidance, see the link in the Guides section).


警告 警告: This is probably only appropriate if your domain is only being used for MediaWiki, but even then it is typically not recommended, as this setup can leave some titles unusable. See Manual:Wiki in site root directory and Cool URIs don't change.

如何创建example.com/Page_title URL:

  • [[Special:MyLanguage/Manual:Short URL/Page title - nginx, Root Access, PHP as a CGI module|Page title - nginx, Root Access, PHP as a CGI module]] Face-smile.svgx6
  • [[Special:MyLanguage/Manual:Short URL/Page title -- Windows & Apache without 403 on Special Pages|Page title -- Windows & Apache without 403 on Special Pages]] Face-smile.svgx3
  • Page title -- Saetta Web服务器


警告 警告: This is probably only appropriate if your domain is only being used for MediaWiki, but even then it is typically not recommended, as this setup can leave some titles unusable. See Manual:Wiki in site root directory and Cool URIs don't change.

如何创建wiki.example.com/Page_title URL。

  • [[Special:MyLanguage/Manual:Short URL/wiki.example.com/Page_title--Subdomain using Lighttpd|wiki.example.com/Page_title--Subdomain using Lighttpd]]



The ampersand problem shows up when you have page titles with symbols in them (such as &, ?, #, + and /) which, 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.[1]


This issue is caused by the ?title=$1 portion of rewrite rules like /index.php?title=$1 which is completely unnecessary and harmful. MediaWiki parses paths directly from the REQUEST_URI so rewrite everything to /index.php.


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 your source url is /wiki with the short url /w, set it to /wiki/load.php.


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.


  1. execute the MySQL query "TRUNCATE objectcache;", or
  1. Ensure $wgCacheEpoch is updated (if $wgInvalidateCacheOnLocalSettingsChange is enabled all you have to do is edit LocalSettings.php to purge it)


Our official guides for short url configuration include recommendations on how to point the root of your site to your wiki using your short url config.

For example our Apache guide recommends using this to point your site root to your wiki:

RewriteRule ^/*$ %{DOCUMENT_ROOT}/w/index.php [L]

Note that we do not recommend doing a HTTP redirect to your wiki path or main page directly. As redirecting to the main page directly will hard-code variable parts of your wiki's page setup into your server config. And redirecting to the wiki path will result in two redirects. Simply rewrite the root path to MediaWiki and it will take care of the 301 redirect to the main page itself.

If using VirtualHosts, place the rewrite rules into the virtual host declaration!


A common beginner's mistake is to install MediaWiki itself (the source code, not the short url) in /wiki instead of /w. Once installed they would realize the mistake when trying to configure Short URLs (which would become difficult as the virtual path then conflicts with the real path).

There is a manual for moving a wiki in general, however that is too complicated if all you need is to change the installation directory (and stay on the same server, same database and same hostname).


  • Backup LocalSettings.php and any other config files you may have created (such as your .htaccess).
  • Rename the installation directory of MediaWiki on your server from /wiki to /w.
  • Set $wgScriptPath in LocalSettings.php to "/w" and remove (or comment out) any definition of $wgArticlePath (this variable will be changed again when setting up Short URLs, further on)
  • At this point your wiki should be working normally at example.org/w
  • Perform a few tests on your wiki to make sure nothing is broken.

Edit a page, visit a few different pages, etc. If anything seems abnormal, revert the changes by restoring your backup of LocalSettings.php and other relevant files you might have changed during configuration of short URLs and rename your folder back to /wiki.

Note for shared hosting users: Many shared hosting plans offer an application manager to automatically Install, Update and Backup applications such as MediaWiki. If your plan has this and if you want to keep using these features, you should contact their tech support and let them know you just manually changed your MediaWiki installation directory.


  • IIS6 Face-smile.svgx1 Face-sad.svgx1
  • [[Special:MyLanguage/Manual:Short URL/Prevent bots from crawling index.php|Prevent bots from crawling index.php]]


  • $wgUsePathInfo - configuration setting that defines whether to use 'pretty' URLs or not
  • $wgActionPaths - configuration setting that you can use to define 'pretty' URLs for other actions, like edit or history


  • MediaWiki Short URL Builder Tool - A tool that can automatically generate a short url configuration more reliably than the configurations in these manual pages.


  1. Bugzilla mod_rewrite for ampersand bug, partially resolved for some users, but only after 13 years