手册:缩短URL/Apache

From mediawiki.org
This page is a translated version of the page Manual:Short URL/Apache and the translation is 75% complete.
Wikipedia 的短链接。

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

设置

开始之前,你需要决定你的伪静态的路径。

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

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

服务器配置

找到正确的文件

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

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

找到Apache配置文件(root权限)

要编辑Root的配置的正确配置文件可能在以下的一处。

正确的配置文件是在 /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 就是你要编辑的。

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

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

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

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

  • 如果您使用Plesk或cPanel,應該有重新啟動伺服器的方法。
  • 命令行中的指令通常是apache2ctl gracefulapachectl graceful/etc/init.d/apache2 restart,最新Fedora發布版為systemctl reload httpd.service,這些指令需要以root執行,通常會在前面加上sudo ...

將.htaccess放在哪

在Apache上,AllowOverride控制是否允許.htaccess控制伺服器配置,如果重寫規則完全沒有運作,您可能需要修改Apache配置中的AllowOverride設定以包含FileInfo,同時在該目錄需要Options FollowSymLinks

如果你使用的是.htaccess文件,你需要编辑或创建这个文件。找到包含你的脚本路径和虚拟路径的路径。通常这意味着你的网站的顶部目录,但让我们看几个例子:

wgScript 示例 wgArticlePath 示例 .htaccess的位置
/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

請注意如果您想要從主域名重新導向到您wiki的首頁(例如http://example.org/ → http://example.org/wiki/Main_Page),您一定要將.htaccess檔案設定在最上層,無論其他目錄嵌套很深。

设置重写规则

在了解Apache语法后会很容易理解剩下的部分,但不适用于整体但Apache文档:

RewriteCond TestString CondPattern
RewriteRule Pattern Substitution [flags]

RewriteCond指令定義的條件必須為真,後面的RewriteRule才會被應用。 可以有多個RewriteCond指令放在RewriteRule指令前面,而且RewriteRule指令前面所有的RewriteCond指令必須都為真,該規則才會應用於URI。 在下面的範例中,TestString使用了引用伺服器變數的格式,例如%{ NAME_OF_VARIABLE }。 雖然有很多種CondPatterns,下面的範例使用-f(當TestString是常規檔案為真)和-d(當TestString是目錄時為真),它們前面都放了否定符號 !

RewriteRule指令都可以在httpd.conf檔案和任何.htaccess檔案中調用,但重新規則出現在.htaccess檔案中時,implicit per-directory context影響規則的Pattern,因為該規則是相對於目前目錄的。在.htaccess檔案中,Pattern並不相對於完整、原始的URI。在.htaccess檔案中,Pattern不應該以斜線(/)開頭,因為URL子字串並不會以/開頭。下面的範例使用了L旗標,表示立即停止重寫程序,不再應用任何規則


在接下來的範例中,mod_rewrite模組必須在Apache或LiteSpeed伺服器中啟用。
如果您使用VirtualHost,請確定該設定放置在VirtualHost指令中

您在配置中需要的第一條規則是啟用重寫引擎:

RewriteEngine On

現在我們需要一條規則,將您的條目路徑變成指向index.php的虛擬路徑,請確定將/wiki/w/index.php替換成您在最開始選擇的路徑(如果您挑選了不同的路徑)。

RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]
如果您決定要自訂這個,確保不要在重寫規則中包含?title=$1或類似東西,包含query將導致MediaWiki內建的處理被覆寫,由於Apache的query重寫被破壞,將導致錯誤,[1] 該目標是配置/index.php的路徑別名,並讓MediaWiki本身根據LocalSettings.php的配置來解析並路由網址。

如果您使用根URL而不是正常的短URL,您需要使用下列的配置(以確保存在的檔案和目錄不會以條目方式顯示,例如「/index.php」和「/images」等):

RewriteCond %{REQUEST_URI} !^/w/rest\.php
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/w/index.php [L]
如果您使用的腳本路徑和條目路徑形如/mywiki/index.php/mywiki/Article,您將需要在您的RewriteRule使用相同的兩行RewriteRule。然而請注意其實沒有好的理由以這樣方式配置您的wiki,如果您的條目路徑已經是子目錄,您只需要將您wiki的腳本路徑移動到另一個目錄,例如/w/index.php/mywiki/Article/my/index.php/mywiki/Article

有時候上面的範例無法運作,下面的範例可能有用(您無法在.htaccess中使用,您需要root存取權限!):

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

您可以選擇包含一個規則以讓路徑本身上顯示首頁,以讓人們在瀏覽不指定條目標題的網址時簡化導航:

# 將 / 重新導向到首頁
RewriteRule ^/*$ %{DOCUMENT_ROOT}/w/index.php [L]

最終結果應該看起來像是:

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

# 啟用重寫引擎
RewriteEngine On

# wiki頁面的短URL
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]

# 將 / 重新導向到首頁
RewriteRule ^/*$ %{DOCUMENT_ROOT}/w/index.php [L]

或者,您可以用「Alias」指令的方式:

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

Alias /wiki /path/to/your/webroot/w/index.php
# 啟用重寫引擎
RewriteEngine On

# 將 / 重新導向到首頁
RewriteRule ^/*$ %{DOCUMENT_ROOT}/w/index.php [L]
請確定保持範例中給定規則的順序,即「wiki頁面的短URL」必須在「將 / 重新導向到首頁」前檢查。
配置中的^/?非常重要,因為不同的Apache配置使用了不同正規表達式,一些要您使用^wiki/,一些要您使用^/wiki//?中的?讓該規則在兩種環境中都能運作。
配置中的%{DOCUMENT_ROOT}確保Apache有正確的無歧義路徑,然而它無法在某些錯誤配置的免費主機中運作,如果您在您的RewriteRules中遇到404或403的問題,移除%{DOCUMENT_ROOT}的部分並重試。
如果您的SCRIPT_PATH指向一些其他的物理位置(Unix中稱為「symlink」、Windows中稱為「shortcut」、Mac OS X中稱為「alias」),您可能需要允許Apache跟隨它,您可以在RewriteEngine上面加入這行:
Options +FollowSymLinks
如果您腳本目錄的路徑被網頁伺服器通過Alias指令設為別名,就像在Debian中的一樣,您只需要加上PT旗標(就是把[L]改為[PT,L]),它將傳遞(Pass Through)請求給其他處理器(mod_alias),將會正確地重新導向該請求,如同Stack Overflow的說明

LocalSettings.php

我們需要在LocalSettings.php使用下列的配置:

## https://www.mediawiki.org/wiki/Manual:Short_URL
$wgScriptPath = "/w";        // 這應該已經以這樣的方式配置
$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" file:

$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";

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:

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 $wgGroupPermissions['*']['read'] to true. Remember to change it back to false once you are done.

See also

References