My Wordpress site's blog post URLs used to look like this:
我的Wordpress网站的博客帖子网址看起来像这样:
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
http://example.com/some-random-blog-post-name http://example.com/other-random-blog-post-name http://example.com/in-case-you-dont-得到了点,更有耐力,全不同
On my new Wordpress site I want them to live in a /blog/ subdirectory where they belong:
在我的新Wordpress网站上,我希望它们存在于它们所属的/ blog /子目录中:
http://example.com/blog/some-random-blog-post-name
http://example.com/blog/some-random-blog-post-name
What's tricky about this is that the old URLs follow no pattern, so I think I need to match against anything unrecognized. Something like this almost works...
这有点棘手的是旧的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>
...but it conflicts with Wordpress's own RewriteRules, and breaks all the other site pages that should pass through unchanged.
...但它与Wordpress自己的RewriteRules冲突,并打破了所有应该通过的其他网站页面。
So how can I achieve the following:
那么我该如何实现以下目标:
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/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/
--> (No redirect)
example.com/contact/ - >(无重定向)
2 个解决方案
#1
1
Inside 404.php
(or 404 handler) of your theme you can use this snippet at start of the file:
在主题的404.php(或404处理程序)中,您可以在文件的开头使用此代码段:
<?php
$url = $_SERVER['REQUEST_URI'];
if (stripos($url, "/blog/", 0) !== 0) {
header("Location: " . "/blog" . $url);
exit;
}
// rest of the 404 code
?>
#2
0
If I were you, I would create a new file 'blog" on my ftp and I would cut and paste all the old wordpress in this new file. This way, you don't need to change anything in .htaccess, redirection will be automatically done in wordpress bo.
如果我是你,我会在我的ftp上创建一个新文件'blog',我会在这个新文件中剪切并粘贴所有旧的wordpress。这样,你不需要改变.htaccess中的任何内容,重定向将是在wordpress bo中自动完成。
#1
1
Inside 404.php
(or 404 handler) of your theme you can use this snippet at start of the file:
在主题的404.php(或404处理程序)中,您可以在文件的开头使用此代码段:
<?php
$url = $_SERVER['REQUEST_URI'];
if (stripos($url, "/blog/", 0) !== 0) {
header("Location: " . "/blog" . $url);
exit;
}
// rest of the 404 code
?>
#2
0
If I were you, I would create a new file 'blog" on my ftp and I would cut and paste all the old wordpress in this new file. This way, you don't need to change anything in .htaccess, redirection will be automatically done in wordpress bo.
如果我是你,我会在我的ftp上创建一个新文件'blog',我会在这个新文件中剪切并粘贴所有旧的wordpress。这样,你不需要改变.htaccess中的任何内容,重定向将是在wordpress bo中自动完成。