今天无意中发现蜘蛛爬行日志中,tag相关内容的返回代码变成了301。复制连接打开后,神奇发现
原来 https://www.yunzhanyou.com/tag/2239.html
而现在变成了https://www.yunzhanyou.com/tag/2239.html/
程序自动把tag内容页 定向成了 /.html/变成了不标准的目录形式。思来想去最近也没动过什么。
仔细研究了半天原来是两段代码冲突。
/* 标签以html方式展示*/
add_action('generate_rewrite_rules','tag_rewrite_rules');
add_filter('term_link','tag_term_link',10,3);
add_action('query_vars', 'tag_query_vars');
function tag_rewrite_rules($wp_rewrite){
$new_rules = array(
'tag/(\d+)/feed/(feed|rdf|rss|rss2|atom).html' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
'tag/(\d+)/(feed|rdf|rss|rss2|atom).html' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
'tag/(\d+)/embed.html' => 'index.php?tag_id=$matches[1]&embed=true',
'tag/(\d+)/page/(\d+).html' => 'index.php?tag_id=$matches[1]&paged=$matches[2]',
'tag/(\d+).html' => 'index.php?tag_id=$matches[1]',
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
function tag_term_link($link,$term,$taxonomy){
if($taxonomy=='post_tag'){
return home_url('/tag/'.$term->term_id.'.html');
}
return $link;
}
function tag_query_vars($public_query_vars){
$public_query_vars[] = 'tag_id';
return $public_query_vars;
}
/* 栏目添加"/"目录形式展现开始*/
function nice_trailingslashit($string, $type_of_url) {
if ( $type_of_url != 'single' )
$string = trailingslashit($string);
return $string;
}
add_filter('user_trailingslashit', 'nice_trailingslashit', 10, 2);
/* 栏目添加"/"目录形式展现结束*/
这两代码不能同时使用,否则就会出现tag标签301的状态。目前云站友只保留了tag以id.html方式展现,至于栏目添加/变成目录,看来我只能考虑借用于插件来解决了!

云站友









评论前必须登录!
立即登录 注册