From mediawiki.org
Jump to navigation Jump to search
This page is a translated version of the page Manual:Short URL/Apache and the translation is 45% complete.
Other languages:
Deutsch • ‎English • ‎español • ‎français • ‎italiano • ‎русский • ‎中文 • ‎日本語 • ‎한국어
Wikipedia 的伪静态效果

这些教程帮助您为 Apache 配置伪静态。 LiteSpeed 服务器软件与 Apache 兼容,可以用完全相同的方式来配置。 关于伪静态是什么或获取对于其他服务器的配置教程,请看手册:短链接 。 如果你使用一些网络寄存(云主机)服务因此不知道你在使用什么服务器软件,你的主机很有可能在使用 Apache。




  • MediaWiki的安装目录在:/w
  • 使用的短链接格式是:/wiki/Page_title


如果您只是想快速配置您的wiki,或者您发现该指南令人困惑,那么您可能需要先尝试该工具。 在做之前,请先编辑/创建 .htaccess 文件。 如果你重新创建,请继续阅读本文中关于where to place the .htaccess file,请注意这个文件可能会和其他需要编辑的文件(LocalSettings.php)在不同的文件夹中,后者在安装文件夹中(/w,如果你遵循MediaWiki的建议)。 但是,请注意,在有防火墙/私人的 wiki 上,它不能工作。 如果你的 wiki 是私人的,你仍可以将 $wgGroupPermissions['*']['read'] 设成 true 把你的 wiki 暂时公开,来尝试配置。 记得在完成之后把它改回 false




在Apache中设置伪静态的推荐方法是编辑Apache配置文件。 这要求您可以访问服务器配置。 如果您在共享主机上,则很可能受到限制,并且需要使用.htaccess文件。 使用.htaccess文件的效率较低,当涉及到多域名的花哨设置时,不会给你那么多的控制权,但它们足够强大,可以设置大多数短网址配置。 LiteSpeed用户需要使用.htaccess方法。




正确的配置文件是在 /etc/apache2/sites-available/ 中设置wiki配置的地方。 大多数linux发行版在设置Apache时都会设置一组 sites-available/sites-enabled/ 文件夹。

如果你没有设置它们,并且在你的wiki设置中使用默认的 /var/www ,那么你可以编辑 /etc/apache2/sites-available/default

不要忘记在 /etc/apache2/apache2.conf 中启用 AllowOverride All 的重写。 在Ubuntu 12.04和Ubuntu 12.10中,它是默认禁用的。

如果你的发行版没有这些目录,那么你应该直接编辑Apache配置文件。 配置文件应该在 /etc/apache2/apache2.conf 。 注意,以前它的名字是 httpd.conf ,如果你有 httpd.conf 而没有 apache2.conf ,那么 httpd.conf 就是你要编辑的。



In an Apache config file you will likely have a VirtualHost block containing your wiki. If you do have one then that is the location where your rewrite rules will go. RewriteRule config does not inherit so don't put these config options in the global config if you are using a VirtualHost.

After you've setup the config as above inside Apache you're going to need to restart Apache to make it apply the new config.

  • If you are using Plesk or cPanel it should have a method of restarting the server.
  • From the command line the command is usually something like apache2ctl graceful, apachectl graceful, /etc/init.d/apache2 restart or as on the latest Fedora releases systemctl reload httpd.service. These commands need to be run as root, usually by prefixing them with sudo ....

Where to put .htaccess

On Apache the AllowOverride controls whether .htaccess files are allowed to control server configuration. If these rewrite rules do nothing at all you may have to modify the AllowOverride setting in the Apache config to include FileInfo. It also requires Options FollowSymLinks for the directory.


wgScript 例子 wgArticlePath 例子 .htaccess location
/w/index.php https://www.mediawiki.org/w/index.php?... /wiki/Page_title https://www.mediawiki.org/wiki/Manual:Short_URL /.htaccess
/w/index.php https://www.mediawiki.org/w/index.php?... /Page_title https://www.mediawiki.org/Manual:Short_URL /.htaccess
/mediawiki/index.php https://www.mediawiki.org/mediawiki/index.php?... /view/Article https://www.mediawiki.org/view/Manual:Short_URL /.htaccess
/mysite/mw/index.php https://www.mediawiki.org/mysite/mw/index.php?... /mysite/view/Page_title https://www.mediawiki.org/mysite/view/Manual:Short_URL /mysite/.htaccess

Note that if you want to make a redirect from the main domain to your wiki's Main Page (e.g. http://example.org/ → http://example.org/wiki/Main_Page). Then you always have to set up the .htaccess file in the top level, even if the other directories are nested deeper.



RewriteCond TestString CondPattern
RewriteRule Pattern Substitution [flags]

The RewriteCond directive defines a condition that must be true before a RewriteRule that follows it may be applied. One or more RewriteCond directives may precede a RewriteRule directive, and all the RewriteCond directives that precede a RewriteRule must be true before that rule may be applied to a URI. In the examples that follow, TestString takes the form of a reference to server variables, e.g. %{ NAME_OF_VARIABLE }. Although many CondPatterns exist, the examples that follow use -f (true when TestString is a regular file) and -d (true when TestString is a directory), and they are preceded by a negation symbol, !.

The RewriteRule directive may be invoked in both the httpd.conf file and in any .htaccess file, but when the rewrite rule appears in .htaccess files, the implicit per-directory context affects the rule's Pattern because rules are relative to the current directory. In .htaccess files, Patterns are not relative to the complete, original URI. For .htaccess files, Patterns should never start with a forward slash, /, because the URI sub-string will never begin with a /. The examples that follow use the L flag whose meaning is Stop the rewriting process immediately, and don't apply any more rules.

The mod_rewrite module must be enabled in Apache or LiteSpeed servers for the examples that follow.
If you are using VirtualHosts, be sure that settings are placed inside the VirtualHost declaration

The first rule you'll need inside of your config is one to enable the rewrite engine:

RewriteEngine On

Now we need a rule to make your article path a virtual path pointing to index.php. Be sure to replace /wiki and /w/index.php with the paths you chose in the beginning (if different).

RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]
If you decide to customize this, be sure to never include ?title=$1 or something like it in the rewrite. Including a query will cause MediaWiki's built in handling to be overridden and will create bugs on your wiki due to the fact that Apache's query rewrites are broken.[1] The goal here is to alias paths to /index.php and then let MediaWiki itself take care of parsing and routing the url, based on the configuration in LocalSettings.php.

If you are using a root url instead of a normal short url you will need to use the following instead (to ensure that existing files and directories are not seen as article, e.g. "/index.php" "/images" etc.):

RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/w/index.php [L]
If you are using a script path and article path that match such as /mywiki/index.php and /mywiki/Article you will also need to use the same two RewriteCond lines on your RewriteRule. However please note that there is no real valid reason to configure your wiki this way. If your article path is already a subdirectory you should just move your wiki's script path to another directory. Such as /w/index.php and /mywiki/Article or /my/index.php and /mywiki/Article.

Sometimes, the above example doesn't work. The following (you can't set this in a .htaccess, you need root access for this!) might work instead:

Alias /wiki /path/to/your/webroot/w/index.php

Optionally, you can include a rule to show the Main Page on the path itself, to simplify navigation when people visit urls without a page title specified:

# Redirect / to Main Page
RewriteRule ^/*$ %{DOCUMENT_ROOT}/w/index.php [L]

The end result should look something like this:

## https://www.mediawiki.org/wiki/Manual:Short_URL/Apache

# Enable the rewrite engine
RewriteEngine On

# Short URL for wiki pages
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]

# Redirect / to Main Page
RewriteRule ^/*$ %{DOCUMENT_ROOT}/w/index.php [L]

or, if you used the way with the "Alias" statement:

## https://www.mediawiki.org/wiki/Manual:Short_URL/Apache

Alias /wiki /path/to/your/webroot/w/index.php
# Enable the rewrite engine
RewriteEngine On

# Redirect / to Main Page
RewriteRule ^/*$ %{DOCUMENT_ROOT}/w/index.php [L]
Make sure that the order of rules as given in the example is being preserved, i.e. "Short url for wiki pages" must be checked prior to "Redirect / to Main Page".
The ^/? in the config is important because different Apache setups use different regexps. Some want you to use ^wiki/ and some want you to use ^/wiki/. The ? in /? allows this rule to work in both contexts.
The %{DOCUMENT_ROOT} in the config ensures that Apache has the correct non-ambiguous path. However it does not work on some badly configured free hosts. If you have 404 or 403 issues with your RewriteRules, remove the %{DOCUMENT_ROOT} parts and try again.
If your SCRIPT_PATH leads to some other physical location (known as "symlink" in Unix, "shortcut" on Windows, "alias" in Mac OS X), you may need to allow Apache to follow those. You can do so by adding this line above the RewriteEngine:
Options +FollowSymLinks
If the path to your script directory is aliased by the webserver via an Alias directive, as it is in Debian among others, the RewriteRule will work as long as you add the PT flag (ie: change [L] to [PT,L]) to Pass Through the request to the next handler - mod_alias, which will correctly redirect the request, as per Stack Overflow


We need to make the following configurations in LocalSettings.php:

## https://www.mediawiki.org/wiki/Manual:Short_URL
$wgScriptPath = "/w";        // this should already have been configured this way
$wgArticlePath = "/wiki/$1";

If you get an "Internal error" page saying "Redirect loop detected!" after you finish configuration you may be using something other than mod_php. If so you need to explicitly turn on short urls using the following line in your LocalSettings.php:

$wgUsePathInfo = true;

If there is still an "internal error" check the log files of your server. Maybe you have to turn on mod_rewrite module.

Simple instructions

The following is a summary of what to do if you are not doing anything different than what MediaWiki recommends. If you want to do something else, you need to read all of the details above.

  • https://example.com/wiki/Main_Page (url location)
  • /home/me/public_html/mediawiki/w/index.html (index.html location)
  • /home/me/public_html/mediawiki/.htaccess (.htaccess location)

Notice that the .htaccess file is NOT located in the "w" directory, but it is located in the directory above it.

.htaccess source code

RewriteEngine On

# main rewrite rule
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]

# Redirect / to Main Page
RewriteRule ^/*$ %{DOCUMENT_ROOT}/w/index.php [L]

LocalSettings.php source code

$wgScriptPath = "/w";     
$wgArticlePath = "/wiki/$1";

See also