[英]Redirect all blog posts from %postname% permalink structure to /news/%postname%
I have changed my permalink to the custom structure /news/%postname%
however I am now trying to set up a 301 redirect so if someone types in the old url of http://example.com/this-is-my-post
they will automatically be redirect to http://example.com/news/this-is-my-post
我已将永久链接更改为自定义结构
/news/%postname%
但是我现在正在尝试设置 301 重定向,因此如果有人输入 http 的旧http://example.com/this-is-my-post
他们将自动重定向到http://example.com/news/this-is-my-post
I have successfully set up a redirect for my custom post types as I moved all the posts over from the custom post type "events" to "latest" by using this:我已经成功地为我的自定义帖子类型设置了重定向,因为我使用以下方法将所有帖子从自定义帖子类型“事件”移动到“最新”:
RewriteRule ^events/(.*) http://www.example.com/latest/$1 [R=301,L]
However I'm not sure I can use this to redirect the main blog posts without affecting the website pages.但是我不确定我是否可以使用它来重定向主要博客文章而不影响网站页面。
To omit the requests for events
or latest
, you must define an appropriate condition . 要省略对
events
或latest
events
的请求,必须定义适当的条件 。 The rule looks similar to the one you already have 该规则看起来与您已有的规则相似
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]
You may also combine the three excluding conditions into one 您也可以将三个排除条件合并为一个
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(events|latest|news)/
RewriteRule ^(.*)$ /news/$1 [R,L]
When everything works as it should, you may replace R
with R=301
( permanent redirect ). 当一切正常运行时,您可以将
R
替换为R=301
( 永久重定向 )。 Never test with R=301
. 切勿使用
R=301
测试。
Step on change your permalink structure in Settings > Permalinks在“设置”>“永久链接”中更改您的永久链接结构
from /%post-name%/
to /news/%post-name%/
Once you are done setting the new link structure Please try this in code you might want copy paste this code in the functions file of your active theme.完成新链接结构的设置后,请在代码中尝试此操作,您可能需要将此代码复制粘贴到活动主题的函数文件中。
https://github.com/devatsemtitans/wp-plugin-snippets/blob/wp_redirect/old-permalinks-redirect https://github.com/devatsemtitans/wp-plugin-snippets/blob/wp_redirect/old-permalinks-redirect
What this code basically does is it caches the slug of your post from the query URL and check if the post exists with that slug in your website then it calls the particular posts permalink and redirects your post to its new URL structure with a 301 status code.这段代码基本上做的是它从查询 URL 中缓存你的帖子 slug 并检查你的网站中是否存在带有该 slug 的帖子然后它调用特定帖子永久链接并将你的帖子重定向到其新的 URL 结构和 301 状态代码.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.