以前网站固定链接结构是这样设置的:/archives/%post_id%,想修改成这样:/archives/%year%/%monthnum%/%post_id%.html,但是网站已经被搜寻引擎收录了好多,以前的地址都无访问了,所以要做好301重定向,跳转到新的固定链接所生成的地址中去:
$rewrite_config = array(); $rewrite_config['highpriority'] = true ; $rewrite_config['rewrite'] = array(); $rewrite_config['oldstructure'] = "/archives/%post_id%"; 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'] = "/archives/%post_id%";
改成你自己的)放到主题的function.php中,再设置好新的固定链接结构就可以了。
本文为原创文章,版权归123ppp资源网所有,欢迎分享本文,转载请保留出处!