如何将旧的Wordpress博客文章URL重定向到新的URL


How to redirect old Wordpress blog posts urls to new ones

我的Wordpress网站的博客文章URL曾经看起来像这样:

http://example.com/some-random-blog-post-name http://example.com/other-random-blog-post-name http://example.com/in-case-you-dont-get-the-point-theyre-all-different

在我的新 Wordpress 网站上,我希望它们位于它们所属的/blog/子目录中:

http://example.com/blog/some-random-blog-post-name

棘手的是旧的 URL 不遵循任何模式,所以我认为我需要与任何无法识别的内容进行匹配。这样的事情几乎有效...

# Redirect unmatched blog posts to /blog/*
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !blog
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteRule ^(.*)$ https://example.com/blog/$1 [L,R=301,NC]
</IfModule>

。但它与Wordpress自己的RewriteRules冲突,并破坏了所有其他应该保持不变的网站页面。

那么我怎样才能实现以下目标:

example.com/some-random-post 301--> example.com/blog/some-random-post example.com/another-random-post 301--> example.com/blog/another-random-post

example.com/contact/ -->(无重定向)

在主题的404.php(或 404 处理程序)中,您可以在文件开头使用此代码段:

<?php
$url = $_SERVER['REQUEST_URI'];
if (stripos($url, "/blog/", 0) !== 0) {
   header("Location: " . "/blog" . $url);
   exit;
}
// rest of the 404 code
?>

如果我是你,我会在我的ftp上创建一个新的文件"博客",我会将所有旧的wordpress剪切并粘贴到这个新文件中。这样,您无需在.htaccess中更改任何内容,重定向将在wordpress bo中自动完成。