Manual:Short URL/Apache/zh

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

設定
开始之前，你需要决定你的伪静态的路径.

在本说明中，我们假设使用如下格式. 记住，在应用到你自己的wiki时，他们可能有所不同.


 * MediaWiki的安装目录在：
 * 使用的短链接格式是：



服务器配置


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

使用以下两个部分之一中的说明，具体取决于您是否具有root权限或使用.



找到Apache配置文件（root权限）
要编辑Root的配置的正确配置文件可能在以下的一处.

正确的配置文件是在  中设置wiki配置的地方. 大多数linux发行版在设置Apache时都会设置一组  和   文件夹.

如果你没有设置它们，并且在你的wiki设置中使用默认的  ，那么你可以编辑.

不要忘记在  中启用   的重写. 在Ubuntu 12.04和Ubuntu 12.10中，它是默认禁用的.

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

如果你的Apache配置不在这些地方，你应该查阅你用来安装Apache的任何系统的文档，并找到Apache配置文件的位置.

如果你在一个共享主机上，无法访问Apache配置文件，你将不得不使用一个.htaccess文件来代替.

在Apache配置檔案中您通常會有一個包含您wiki的VirtualHost區塊，如果確實有一個，這就是您重寫規則應該放的地方. RewriteRule配置不會繼承，如果您使用VirtualHost，請不要將這些配置選項放在全域配置.

在您已上述方法設定好Apache的配置時，您需要重新啟動Apache以應用新的配置.


 * 如果您使用Plesk或cPanel，應該有重新啟動伺服器的方法.
 * 命令行中的指令通常是 、 或 ，最新Fedora發布版為 ，這些指令需要以root執行，通常會在前面加上.



將.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.



设置重写规则
在了解Apache语法后会很容易理解剩下的部分，但不适用于整体但Apache文档:

The  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,  takes the form of a reference to server variables, e.g.. Although many  exist, the examples that follow use   (true when TestString is a regular file) and   (true when TestString is a directory), and they are preceded by a negation symbol,.

The  directive may be invoked in both the   file and in any   file, but when the rewrite rule appears in .htaccess files, the implicit per-directory context affects the rule's   because rules are relative to the current directory. In  files, Patterns are not relative to the complete, original URI. For  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  flag whose meaning is Stop the rewriting process immediately, and don't apply any more rules.

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

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

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. " " " " etc.):

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

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:

The end result should look something like this:

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

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

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

If there is still an "internal error" check the log files of your server. Maybe you have to turn on  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.

Online Wizard
It is possible that this wizard will help people generate the short URLs they need. (NOTE: This needs to be tested, or the section removed.)

An automated wizard (requires MediaWiki 1.17 or later) might help:


 * shorturls.redwerks.org

If you just want your wiki configured quickly or you find the guide confusing then you may want to try that tool first. If you do this, please note that you will need to edit or create a .htaccess file. If you create it anew, please read on to further in this article about where to place the .htaccess file, and please note that this file will likely be in a different folder than the other file that will need to be edited (LocalSettings.php), which is in the installation folder (/w if you have followed the MediaWiki recommendations). Please also note that using this wizard will not work on firewalled or private wikis. In this case, you can still try it out by making your wiki temporarily public by setting  to. Remember to change it back to once you are done.