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