在 MediaWiki 中配置 短网址(Short URL),让原本长长的 URL(例如 https://example.com/index.php?title=页面名)变成更简洁的形式(例如 https://example.com/页面名)。
原理:通过 Web 服务器重写规则(Apache 的 .htaccess 或 Nginx 的 rewrite)把简短 URL 重定向到 index.php,然后由 MediaWiki 解析。
配置步骤
环境:Ubuntu 22.04;Apache
步骤 1:修改 LocalSettings.php
打开 LocalSettings.php
添加或修改以下配置:
# 设置短网址
$wgScriptPath = "/w";
$wgArticlePath = "/w/$1";
$wgUsePathInfo = true;
步骤 2:Apache 配置(.htaccess)
如果你用 Apache,确保启用 mod_rewrite,
vi /var/www/html/w/.htaccess:
RewriteEngine On
# 这行让 Apache 知道 MediaWiki 主入口脚本
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]
# 如果你想直接 /w/页面名
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
重启 Apache
sudo systemctl reload apache2