[英]Redirect all blog posts from %postname% permalink structure to /news/%postname%
我已将永久链接更改为自定义结构/news/%postname%
但是我现在正在尝试设置 301 重定向,因此如果有人输入 http 的旧http://example.com/this-is-my-post
他们将自动重定向到http://example.com/news/this-is-my-post
我已经成功地为我的自定义帖子类型设置了重定向,因为我使用以下方法将所有帖子从自定义帖子类型“事件”移动到“最新”:
RewriteRule ^events/(.*) http://www.example.com/latest/$1 [R=301,L]
但是我不确定我是否可以使用它来重定向主要博客文章而不影响网站页面。
要省略对events
或latest
events
的请求,必须定义适当的条件 。 该规则看起来与您已有的规则相似
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/events/
RewriteCond %{REQUEST_URI} !^/latest/
RewriteCond %{REQUEST_URI} !^/news/
RewriteRule ^(.*)$ /news/$1 [R,L]
您也可以将三个排除条件合并为一个
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(events|latest|news)/
RewriteRule ^(.*)$ /news/$1 [R,L]
在“设置”>“永久链接”中更改您的永久链接结构
from /%post-name%/
to /news/%post-name%/
完成新链接结构的设置后,请在代码中尝试此操作,您可能需要将此代码复制粘贴到活动主题的函数文件中。
https://github.com/devatsemtitans/wp-plugin-snippets/blob/wp_redirect/old-permalinks-redirect
这段代码基本上做的是它从查询 URL 中缓存你的帖子 slug 并检查你的网站中是否存在带有该 slug 的帖子然后它调用特定帖子永久链接并将你的帖子重定向到其新的 URL 结构和 301 状态代码.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.