今天BTV导航网的小编为你讲一下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 = 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重定向的方法类似的相关内容推荐,可以你更全面的帮助你解决问题。我们BTV85网址导航还提供网址收录服务,你可以注册提交你的网站信息,帮你引导搜索引擎蜘蛛,同时还有网站SEO优化交流微信群,里面很多SEO高手和大咖,加友链,可以免费进群。