【发布时间】:2016-06-27 04:46:38
【问题描述】:
我的 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-post301--> example.com/blog/another-random-post
example.com/contact/ -->(无重定向)
【问题讨论】:
-
哪些 URL 模式应该重定向到
/blog,哪些不应该? -
@anubhava 我认为上面的问题已经解释过了。旧博客文章没有特定的模式,因为它们没有以子目录为前缀。所以基本上我们需要重定向任何不是现有 Wordpress 页面的内容。
-
在这种情况下 mod_rewrite 不是解决方案。需要在 WP 本身中处理。
-
@anubhava 我全神贯注。你有什么建议
-
可以看看RewriteMap。
标签: php wordpress .htaccess redirect mod-rewrite