【问题标题】:Mediawiki redirect from old URL path starting with /w/ to new path without itMediawiki 从以 /w/ 开头的旧 URL 路径重定向到没有它的新路径
【发布时间】:2022-01-04 05:15:02
【问题描述】:

我有所有页面的旧 URL 路径

example.com/w/Page_title

现在我改成

example.com/Page_title

使用Short URL manual for Apache

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

我的 LocalSettings.php

$wgForceHTTPS = true;

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

和 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]

【问题讨论】:

    标签: apache url redirect mediawiki


    【解决方案1】:

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

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

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

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

    最后它应该看起来像你的其他规则:

    RewriteEngine On
    # Redirect old URLs that start with /w/
    RewriteRule ^\/?w\/(.*) /$1 [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]
    
    

    【讨论】:

    • 谢谢@MrWhite 我补充说
    猜你喜欢
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 2012-02-01
    • 1970-01-01
    • 2021-04-29
    相关资源
    最近更新 更多