简体   繁体   English

将所有博客文章从 %postname% 永久链接结构重定向到 /news/%postname%

[英]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 . 要省略对eventslatest 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.

相关问题 Wordpress Nginx永久链接从纯名称到邮政名称 - Wordpress Nginx Permalink from plain to postname 分页不适用于固定链接的邮政名 - pagination not working with permalink postname 将永久链接结构更改为postname时,Wordpress localhost产生404页 - Wordpress localhost results in 404 pages when changing permalink structure to postname WordPress%postname%permalink返回404 - WordPress %postname% permalink returns 404 手动将 WordPress 数据库从域转移到另一个域,在固定链接中使用 postname - Manually transfer WordPress database from domain to another domain with postname in permalink 如何在永久链接设置为“/%postname%/”的情况下从 URL 中删除类别库 - How to remove category base from URL with permalink set to “/%postname%/” 设置为postname时,WordPress永久链接不起作用 - Wordpress permalink is not working when set to postname 如何设置一个永久链接结构(如wordpress),域名后面仅加上邮政名称? - How to setup a permalink structure like wordpress with just postname after the domain name? 是否可以将自定义帖子类型永久链接结构更改为使用%post_id%而不是%postname%? - Is it Possible to Change a Custom Post Type Permalink Structure to Use %post_id% instead of %postname%? 301 从 /%year%/%monthnum%/%postname%.html 重定向到 /%postname%/ - 301 Redirection from /%year%/%monthnum%/%postname%.html to /%postname%/
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM