Mediawiki 从以 /w/ 开头的旧 URL 路径重定向到没有它的新路径

Mediawiki redirect from old URL path starting with /w/ to new path without it

我有所有页面的旧 URL 路径

example.com/w/Page_title

现在我改成了

example.com/Page_title

使用Short URL manual for Apache

问题是: 如何为使用书签返回的用户从旧路径进行 301 重定向?

我的LocalSettings.php

$wgForceHTTPS = true;

$wgScriptPath = "/wiki";
$wgArticlePath = "/";

和mod_rewrite:

RewriteEngine On
# Short URL for wiki pages
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/index.php [L]
# Redirect Main Page to /
RewriteRule ^/*$ /index.php?title=Main_Page [L,QSA]
RewriteRule .*\.ico$ - [L]

您需要添加 RewriteRule 以在其他规则之前进行重定向:

RewriteRule ^\/?w\/(.*) / [R=301,L]

这匹配任何以 /w/ 开头的 URL 并重定向以将其删除。

  • ^ - 开头为
  • \/? - 一个可选的起始斜杠,因此该规则将适用于不同的上下文(Apache conf 和 .htaccess)
  • w\/ - w/ 目录
  • (.*) - 在目标
  • 中变为 </code> 的捕获组中 <code>w/ 之后的所有内容
  • / - 重定向到哪里
  • R=301 将其设为永久 (301) 重定向
  • L - 将此设置为最后一个重写规则,以便在匹配时不会处理其他规则。

你的其他规则最终应该是这样的:

RewriteEngine On
# Redirect old URLs that start with /w/
RewriteRule ^\/?w\/(.*) / [R=301,L]
# Short URL for wiki pages
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/index.php [L]
# Redirect Main Page to /
RewriteRule ^/*$ /index.php?title=Main_Page [L,QSA]
RewriteRule .*\.ico$ - [L]