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%/";

function 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'])) {
// ok, we need to generate a 301 Permanent redirect here.
header("HTTP/1.1 301 Moved Permanently", TRUE, 301);
header('Status: 301 Moved Permanently');
$permalink = get_permalink($post[0]->ID);
if (is_feed()) {
$permalink = trailingslashit($permalink) . 'feed/';
}
header("Location: ". $permalink);
exit();
}
}
return $post;
}

function 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) {
return array_merge($rewrite_config['rewrite'], $rules);
} else {
return array_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');

将我的$rewrite_config['oldstructure'] = “/%year%/%monthnum%/%postname%/”前面的/%year%/%monthnum%/%postname%/修正成你本人的旧的固定链接格局,而后将这段代码退出到主题的function.php中,而后设置成新的固定链接格局就搞定了.

以上就是安达网络工作室关于《wordpress修改固定链接后301重定向的方法》的一些看法。更多内容请查看本栏目更多内容!

本文相关话题: wordpress 固定链接 301
版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
WordPress中Gravatar头像缓存到本地及相干优化的技巧

将Gravatar寰球通用头像缓存的目的在于放慢网站的关上速度,由于Gravatar官网的效劳器在国外,加上伟大的GF...

wordpress歹意代码处理办法分享

明天莫明其妙发现本人的测试站的主题带上了歹意代码,十分显著的就是呈现了一个函数_verifyactivate_widget...

WordPress评论增加楼层显示的办法小结

本文实例总结了WordPress评论增加楼层显示的办法。分享给大家供大家参考。详细剖析如下: 第一步:要在评论...

wordpress模板罕用函数集锦

根本模板文件 style.css : CSS(款式表)文件index.php : 主页模板archive.php : Archive/Category模板404.ph...

wordpress自定义循环列表的php代码

复制代码代码如下: <?php $epaged = (get_query_var('paged')) ? get_query_var('paged') : 1; $temp = $wp...

wordpress上一页下一页无插件分页函数实例

本文实例讲述了wordpress上一页下一页无插件分页函数。分享给大家供大家参考。详细剖析如下: 这里提供两个...

需求提交

客服服务

亿鸽在线客服系统