Manual:Short URL/zh-hans

From MediaWiki.org
Jump to: navigation, search
以下的一些资料可能会过时或不正确。如果你熟悉以下的内容,请尝试帮助我们更新这篇文章。

万维网的发明者蒂姆伯纳斯-李曾经说过,良好的网页URL地址应该永远保持不变[1]。隐藏了复杂的程序代码的网页地址能给访客一个好的体验。在开始之前,请花几分钟时间为您的网站制订一个稳定的网址结构,以减少以后一些不必要的麻烦[2]。为了阅读方便,这个页面已经被分成独立的章节,下面有目录。

Contents

[edit] 简介

[edit] 默认配置

MediaWiki的默认安装路径一般都是下面这样:

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

MediaWiki的默认页面地址一般是下面这样:

http://example.com/w/index.php/Page_title (最新版本的Mediawiki, 没有使用CGI)
http://example.com/w/index.php?title=Page_title (最新版本的Mediawiki, 使用了CGI)

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

http://example.com/wiki/Page_title ( 标准形式,像Wikipedia那样的)
http://wiki.example.com/Page_title (不建议采用!)

[edit] 好处与坏处

Long URLs have a key advantage: they work in all hosting environments — a good thing to know lest one day you move your wiki to a hosting environment where you can't keep using the same URLs everyone has bookmarked or linked to.

The advantages of short URLs are that they hide all technical details, as is best practice for URLs[3] – among other things this means they can be kept stable when the underlying mechanism change, if your hosting environment supports this. They are also easier for visitors to remember and allow visitors to navigate the site by URL more conveniently.

Once you've decided that you really want short URLs and are willing to go through the effort, you're ready for the long trial and error configuration as detailed below.

[edit] Shared hosting

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[4]). You only need to edit either .htaccess or httpd.conf, not both.

[edit] 建议的配置步骤(以 Wikipedia 的配置为例)

下面的配置过程已为 Wikipedia 所采用,且倾向于健壮和容易搭建。除非你实在不喜欢它的某些方面(比如没有重写全部 URL)或者你干脆不能这样操作(需要 root 用户登录),你应该遵循这个配置过程。

[edit] 优势

  • 本方法是可靠的,且被保证将在 MediaWiki 未来版本可以使用的。MediaWiki 的开发者不会测试其它方案。当软件有改动时,其它方案可能会失败。
  • 没有重写全部 URL ,但这很好。有这么一个简单的规则,很容易地阻止了搜索引擎的爬虫查看后台页面(比如编辑表单或历史页面),在日志分析中分离了查看内容和其他访问,等等。尽管到编辑页和后台页的链接有点难记,这没什么大不了:重要的是内容。
  • 本方法使用别名而不是重写。这更简单也更可靠。

[edit] 缺陷

  • 这个方法需要 Apache 以及 root 用户登录。

[edit] 配置步骤

首先,确保你用的是 Apache 并且有 root 访问权限(这样你才能修改httpd.conf,修改.htaccess是不够的)。如果任意一个不行,你就不能用这个方法。你需要对其进行相应的改动,或使用后面列出的其他方法。建议Manual:Short URL/wiki/Page title -- no root access

  1. 找到你 wiki 安装的目录。缺省情况下,是个叫做 /w 的子目录,位于你的 web 服务器的文件根目录(网络根目录)。这个根目录是你服务器上所有公开的 html 文件所在的目录。例如,如果你的网络根目录上 /var/www,那么你的 wiki 可能在 /var/www/w。在这个目录中,你可以看到一些文件,如 index.php 和 LocalSettings.php 。这可能让你有点糊涂,因为在一些系统(如:Debian),子目录 /w 可能只是个符号链接(如:到 /usr/share/mediawiki/)。
  2. 选择一个你想让你的文章出现的虚拟目录。下面假定你选了 /wiki/(和 Wikipedia 一样)。文章将被这样访问 http://www.example.com/wiki/文章标题别建这个虚拟目录,它不能在网络根目录中存在!下面假定,和 Wikipedia 一样,你把 MediaWiki 安装到了网络根目录的 /w/ 子目录,而不是 /wiki/ 目录。
    Note Note: 倘若之前你不知道,选择 /wiki/ 作为 MediaWiki 的安装目录,那就把它改个新名字(这里是 /w/)并把你看到的所有 /wiki/ 替换为新目录(这里是 /w/)(通常只有一个 LocalSettings.php 文件要修改)。
    Note Note: 安装目录一定不能和虚拟目录相同,也一定不能是网络根目录,第一点很重要,不要试着忽略它来让 URL 漂亮一点,否则这样配置不会成功。
  3. LocalSettings.php ,做如下改动:
    $wgScriptPath = "/w";         # 实际文件的路径
    
    $wgArticlePath = "/wiki/$1";  # 虚拟路径,*一定*要和 $wgScriptPath 中不同
    
    $wgUsePathInfo = true;        # 启用美化的 URL
    
    注意 $wgScriptPath 应已被设置成 /w。如果没有(比如,你正从另一 URL 方案迁移),先去把它设成那样。
  4. 编辑 httpd.conf ,使其包含规则 Alias /wiki /path/to/your/wiki/index.php。注意第二个路径是基于文件系统的根目录,而不是网络根目录!例如,如果 /var/www/ 是你的网络根目录,这里可能是 Alias /wiki /var/www/w/index.php。如果这样设置不行或返回了 403 错误,设一个 MediaWiki 安装的绝对路径。比如 C:/absolute/path/to/your/wiki/index.php。这个别名设置是个最小化的配置。想了解更多的选项,参见 Apache configuration 。如果你在用 Apache 2 ,配置文件叫 apache2.conf。如果你那里没有 httpd.conf,请使用这个
  5. 让 Apache 重新读取配置文件(优雅地重启)。比如,你可能以 root 用户调用 apachectl graceful 或一个类似的命令,或使用WebHost Manager或其他类似软件的特性。如果你在用 Apache 2 ,该命令是 apache2ctl graceful
  6. 在你的网络根目录,如果没有名为 robots.txt 的文件,创建一个。然后把这几行加入文件末尾:
    User-agent: *
    
    Disallow: /w/
    
    Disallow: /wiki/Special:Search
    
    Disallow: /wiki/Special:Random
    
    第一个“Disallow”规则阻止网络爬虫检索历史记录、编辑页面和其他无益于用户搜索的后台页面。也防止了重复的内容被索引到。第二、三个规则阻止网络爬虫检索两个可能使其困惑并一般来说无用的页面。
    Note Note: 如果你在用一个本地化版本的 MediaWiki(非英文版),这两个特殊页面的名字是翻译过的。检查你 wiki 中的实际链接以便得到特殊页面的 URL 。它们与用户的个性化语言设置无关。

完成了,你的 wiki 应该已经完美地工作了。如果没有,到 irc://irc.freenode.org/mediawiki 报告任何问题。

[edit] 其他方式的缩写配置

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 示例: example.com/wiki/Page_title

How to create example.com/wiki/Page_title URLs: Manual:Short URL/zh-hans/guide - recommended method if you don't have root access; should also work with PHP as CGI mode Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide 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.

Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide (If the Alias method is not suitable [for example, you use PHP as a CGI], you can use Apache instead.) Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide

[edit] URL 示例: example.com/Page_title

How to create example.com/Page_title URLs: Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide (used this method: Face-sad.svg x2) Manual:Short URL/zh-hans/guide (also works with 1.10 & 1.13, but not with 1.11., very similar to the above solution) Manual:Short URL/zh-hans/guide

[edit] URL 示例: example.com/wiki.php/Page_Title

Manual:Short URL/zh-hans/guide

[edit] URL 示例: example.com/sub_directory/wiki/Page_Title

Manual:Short URL/zh-hans/guide

[edit] URL 示例: wiki.example.com/Page_title

How to create wiki.example.com/Page_title URLs. Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide (the above enhanced, work with 1.14) Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide

[edit] Troubleshooting

[edit] Ampersand (&) problem

The ampersand problem shows up when you have page titles with symbols in (such as &, ?, #, + and /) that, 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. There are discussions of other possible solutions at lists.wikimedia.org and fgiasson.com.

Solutions: Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide

[edit] Looping alias/rewrite errors

If you receive looping alias/rewrite errors such as "Cannot find page www.example.com/wiki/wiki/wiki/wiki/wiki/ [...] /index.php", try one of these fixes:

Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide

[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".

[edit] Revert to default

If you need to revert to the default values of your wiki but have accidentally deleted them, here are the defaults:

$wgScriptPath       = "/wiki";
$wgScript           = "$wgScriptPath/index.php";
$wgRedirectScript   = "$wgScriptPath/redirect.php";
 
## If using PHP as a CGI module, the ?title= style usually must be used.
# $wgArticlePath      = "$wgScript=$1";
 
$wgArticlePath      = "$wgScript?title=$1";

[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.
?>

[edit] Tricks

Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide Manual:Short URL/zh-hans/guide

[edit] See also

[edit] External links

[edit] References

  1. Cool URIs don't change by Tim Berners-Lee. (Interestingly, the short URLs discussed on this page are the ones that will break if one day one moves to a system where only the long URLs work!)
  2. 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. (One might argue that long URLs are the most "stable" ones, being immune to hosting company environment changes, etc.)
  3. Hypertext Style: Cool URIs don't change
  4. 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.


语言: English  • 日本語 • Русский • ‪中文(简体)‬
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox