在 .htaccess 中将 /blog/article 重定向到 /articles/article

Redirect /blog/article to /articles/article in .htaccess

正如标题所说,我想从我的旧博客 URL 重定向到新博客。这是我尝试过的:

RewriteRule ^/blog/(.*)$ /articles/ [R=301,NC,L]

我真的认为这会奏效,但事实证明我遗漏了一些东西。如果你能帮忙,那就太好了。

URL-路径与每个目录上下文中的RewriteRule模式相匹配(即在.htaccess中)永远不会以斜杠开头,因为目录前缀(以斜杠结尾)首先被删除。换句话说,正则表达式 ^/blog/(.*)$ 永远不会匹配。

您需要类似以下内容:

RewriteRule ^blog/(.*) /articles/ [R=301,NC,L]

请注意 模式 开头没有斜杠。这与在 server(或 virtualhost)上下文中使用 mod_rewrite 时形成对比,然后需要斜杠前缀。