WordPress主题站301重定向设置

WordPress主题站301重定向设置,第1张

WordPress主题站设置301重定向是为了避免网站在修改后台固定链接后,没有设置好访问新链接页面后出现404状况,所以我们就需要设置好301重定向:

WordPress主题站301重定向设置,第2张

$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中,然后设置成新的固定链接格式就搞定了。

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » WordPress主题站301重定向设置

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情