wordpress修改固定链接后301重定向的方法

wordpress修改固定链接后301重定向的方法,第1张

wordpress="superseo">ss修改固定链接后301重定向的方法

本文主要介绍wordpress修改固定链接后301重定向的方法。有需要的朋友可以参考一下。

以前小站的固定链接都那么没有人情味,结构都是/%year%/%monthnum%/%postname%/,看起来不直观,很长。今天我做了一个301重定向,然后修改了新的固定链接。现在固定链接的格式是/%postname%。html,这使得它们看起来更短更直观。
但是很多小站都被搜索引擎收录了,之前的地址都无法访问。所以,做好301重定向,跳转到新的固定链接生成的地址。否则如果都是404就悲剧了。下面是解决方案。


复制代码如下:
$rewrite_config=array();
$rewrite_config['highpriority']=true;
$rewrite_config['rewrite']=array();
$rewrite_config['oldstructure']="/%year%/%monthnum%/%postname%/";

函数wpdaXue_pm_the_posts($post){
global$WP;
global$WP_rewrite;
global$rewrite_config;

$rewrite_config['rewrite']=$WP_rewrite->;generate_rewrite_rule($rewrite_config['oldstructure'],false,true,true,true);
if($post!=NULL&&is_single()&;&$rewrite_config['oldstructure']!=$WP_rewrite->;permalink_structure){
if(array_key_exists($WP->;matched_rule,$rewrite_config['rewrite']){
//好了,我们这里需要生成一个301永久重定向。
header("HTTP/1.1301永久移动",TRUE,301);
header('Status:301永久移动');
$permalink=get_permalink($post[0]->;ID);
if(is_feed()){
$permalink=trailingslashit($permalink)。feed/';
}
header("Location:"。$permalink);
exit();
}
}
return$post;
}

函数wpdaXue_pm_post_rewrite_rules($rules){
global$WP_rewrite;
global$rewrite_config;
$oldstruct=$rewrite_config['oldstructure'];

if($oldstruct!=NULL&&$oldstruct!=$WP_rewrite->;permalink_structure){
$rewrite_config['rewrite']=$WP_rewrite->;generate_rewrite_rule($oldstruct,false,true,true,true);
if($rewrite_config['highpriority']==true){
returnarray_merge($rewrite_config['rewrite'],$rules);
}else{
returnarray_merge($rules,$rewrite_config['rewrite']);
}
}
return$rules;
}
add_filter('the_posts','wpdaxue_pm_the_posts',20);
add_filter('post_rewrite_rules','wpdaXue_pm_post_rewrite_rules');

将my$rewrite_config['oldstructure']="/%year%/%monthnam%/"后的/%year%/%monthnam%/"修改成自己的旧固定链接格式,然后将这段代码添加到主题的function.php中,再将其设置为新的固定链接。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/772497.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-03
下一篇2022-05-03

发表评论

登录后才能评论

评论列表(0条)

    保存