如何将旧的Wordpress博客帖子重定向到新的

eme*_*his 7 php wordpress .htaccess mod-rewrite redirect

我的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>
Run Code Online (Sandbox Code Playgroud)

...但它与Wordpress自己的RewriteRules冲突,并打破了所有应该通过的其他网站页面.

那么我该如何实现以下目标:

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

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

anu*_*ava 1

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

<?php

$url = $_SERVER['REQUEST_URI'];

if (stripos($url, "/blog/", 0) !== 0) {
   header("Location: " . "/blog" . $url);
   exit;
}

// rest of the 404 code
?>
Run Code Online (Sandbox Code Playgroud)