过年一直没空写文章,今天给大家分享一篇折腾经验教程。好几年前我就觉得以分类名/文章id这样的命名链接有点不好,由于考虑到已经有很多文章被收录了,就一直没改,上周发神经改了固定链接,导致几乎所有收录都变成了404,本以为百度站长的网站改版很好用,实际上卵用没有。就知道说无需改版,我TM都404了还不需要改版。
忠告:WordPress固定链接建议使用域名/postid.html的形式,即使以后你修改了分类目录,也不会影响到文章的访问!切记!
我之前采用的是域名/分类名/postid.html的链接形式,改成域名/postid.html的形式,修改很简单,只需要在WordPress后台固定链接里设置下就行了,关键是搜索引擎它不听话呀(这里指百度)。
本来想等百度自己发现错误修改错误的,目前已经有一部分改了过来,但今天听到群里一位站长说他都改了2个多月了,仍然一大片的404我觉得还是得采取点手段拯救下。
wp-permalinks-migration插件,WordPress大学出品,专门为修改固定链接所做,使用简单快捷,各种语言保证你认识,文末有资源地址。
只需要输入你曾经用过的链接形式即可,它的功能就是判断你访问的链接是不是老链接,是就301重定向到新链接,不是就不动作。这个插件的代码也很简单,只有两个核心代码,一个是WordPress固定链接规则,另一个就是重定向逻辑了,简单修改下放入functions.php中就可以省去插件。
function wpdaxue_pm_the_posts($post)
{
global $wp;
global $wp_rewrite;
global $wpdaxue_pm_config;
if ($post != NULL && is_single() && $wpdaxue_pm_config['oldstructure'] != $wp_rewrite->permalink_structure)
{
if (array_key_exists($wp->matched_rule, $wpdaxue_pm_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 $wpdaxue_pm_config;
$oldstruct = $wpdaxue_pm_config['oldstructure'];
if ($oldstruct != NULL && $oldstruct != $wp_rewrite->permalink_structure)
{
$wpdaxue_pm_config['rewrite'] = $wp_rewrite->generate_rewrite_rule($oldstruct, false, false, false, true);
update_option("wpdaxue_pm_options",$wpdaxue_pm_config);
if ($wpdaxue_pm_config ['highpriority'] == true)
{
return array_merge($wpdaxue_pm_config['rewrite'],$rules);
}
else
{
return array_merge($rules, $wpdaxue_pm_config['rewrite']);
}
}
return $rules;
}让我没想到的是,百度收录的链接千奇百怪,下面这种地址都有,不过不用担心,wp-permalinks-migration插件仍然可以正确重定向。
目前也只能先开着这个插件,直到哪天百度完全修改过来再关掉了,前途一片黑暗!







评论 (10)