Manual:Short URL/zh

Short URLs or URL Rewrites hide php file extensions from the page address. Please take a few minutes to devise a stable URL structure for your website before getting started, to reduce problems later.

目标
MediaWiki的默认安装路径一般都是下面这样:
 * (用Root账户安装)
 * (虚拟主机安装)

MediaWiki的默认页面地址一般是下面这样：
 * （Mediawiki的最新版本，无CGI支持）
 * （Mediawiki的最新版本，有CGI支持）

使用下面将提到的方法, 我们可以把地址设置成下面的样子:
 * This is the most common configuration, same as in Wikipedia, though not the default because it requires server side modifications
 * （不推荐使用！参见下方）
 * （不推荐使用！参见下方）
 * （不推荐使用！参见下方）
 * （不推荐使用！参见下方）

Having the page name right in the root of the domain name is not recommended, because it will conflict with files and directories located at the root directory. For example, if you have your images in the /images/ directory, you wouldn't be able to access a page named "Images" in your wiki.

优势与劣势

 * 优势：缩短URL会隐藏所有技术细节，对URL来说是最佳的处理方式. 这意味着如果你的主机环境支持的话，当相关机制变化时，他们依然是稳定的. 对访问者来说，他们也更易于记忆并且让访问者更方便的根据URL进行导航.  also, short URLs are better than default one to SEO because URLs includes   is minus to SEO.
 * Disadvantage: requires configuration, so, depending on the hosting environment you are using, it might be difficult to recreate. However, as every widely used webserver is capable of being configured to use short urls, practically every paid web host should support this feature. Any host that doesn't offer you this feature is not worth your money.

指导
Our official guides for short URL configuration for different web servers. Pick your webserver's configuration from this list. If you don't know what server software you are using, then it is most likely Apache



其他教学指导

 * These guides are old and are almost entirely bad advice. These will eventually be deleted one by one as our official guides above are created for different webservers.

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.

URL示例——example.com/wiki/Page_title
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 after clicking on "I don't have root access".

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

URL示例——example.com/Page_title
如何创建example.com/Page_title URL：

URL示例——wiki.example.com/Page_title
如何创建wiki.example.com/Page_title URL.

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.

解决方法：

This issue is caused by the  portion of rewrite rules like   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 you source url is /wiki with the short url /w, set it to /wiki/load.php.

Purging cache
If you notice that your changes to  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.

Also you can:
 * 1) execute the MySQL query "TRUNCATE objectcache;", or
 * 2) Ensure  is updated (if  is enabled all you have to do is edit LocalSettings.php to purge it)

Redirect visitor of your domain to your wiki
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:

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!

Moving a wiki from /wiki to /w
A common beginner's mistake is to install MediaWiki itself (the source code, not the short url) in  instead of. 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).

Here is what you have to do:


 * Backup  and any other config files you may have created (such as your  ).
 * Rename the installation directory of MediaWiki on your server from  to.
 * Set  in   to   and remove (or comment out) any definition of   (this variable will be changed again when setting up Short URLs, further on)
 * At this point your wiki should be working normally at
 * Now go through the regular guide to set up short URLs
 * 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  and other relevant files you might have changed during configuration of short URLs and rename your folder back to.

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.

参见

 * Explanation of what CGI modules are
 * $wgUsePathInfo - configuration setting that defines whether to use 'pretty' URLs or not
 * Notes on placing your wiki installation in site root directory
 * $wgActionPaths - configuration setting that you can use to define 'pretty' URLs for other actions, like edit or history
 * Clean up URLs RFC - proposes "cleaner URLs for editing, previewing skins, etc."

外部链接

 * Apache Module mod_rewrite at Apache.org
 * MediaWiki Short URL Builder Tool - A tool that can automatically generate short url configuration more reliable than the configurations in these manual pages.