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

标签 php wordpress .htaccess redirect mod-rewrite

我的 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
?>

关于php - 如何将旧的 Wordpress 博客文章 URL 重定向到新的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35948704/

相关文章:

php - 加速一个(缓慢的)巨大的 wordpress 数据库

wordpress - 返回自定义帖子类型中已发布帖子的数量

apache - 在 htaccess 重定向中强制使用非 www 和 HTTPS 会导致重定向过多

.htaccess - htaccess 将子域重定向到子目录

php - PHP 中使用端口号和根文件夹重写 URL

PHP 文件传输 - 这是如何工作的?

html - Bootstrap 网格未对齐

java - 我们如何管理 SMS,哪种语言是最好的处理方式?

php - fatal error : Call to undefined function ldap_connect() in wamp

javascript - Laravel 和隐藏输入