wordpress全站链接(URL)规范化优化方案 category存档页优化tag 存档页优化

在SEO中网站的链接结构以及目录层次都非常重要,百度官方的SEO白皮书内就强调了,一个好的URL结构不仅能让搜索引擎更加容易了解到网站内 容,而且也利于访客记忆。很多SEO书籍中都提到了,网站URL的目录层次最好不要超过三层。当然如果URL中包含关键词就更好了。那么wordpress网站该如何设定网站的伪静态才能符合SEO标准呢?

文章页:

应采用“/%category%/%post_id%.html”格式的链接,如下图:

20151106191221

采用这样输出的网址就是“http://域名/分类名/“文章唯一id   都说接在域名后面的第一个/后面的东西对于搜索引擎权重越大,其实不然,这样反而会让网站结构变得杂乱不堪,wordpress的默认的结构是“http://域名/category/分类/“  由此可见域名/后面应该接的就是分类了,可是这里却有了一个category 所以所以需要去掉category 达到效果 变成http://域名/分类/

把这段代码加到主题里面的functions.php里面:

add_action( 'load-themes.php',  'no_category_base_refresh_rules');
add_action('created_category', 'no_category_base_refresh_rules');
add_action('edited_category', 'no_category_base_refresh_rules');
add_action('delete_category', 'no_category_base_refresh_rules');
function no_category_base_refresh_rules() {
    global $wp_rewrite;
    $wp_rewrite -> flush_rules();
}
// register_deactivation_hook(__FILE__, 'no_category_base_deactivate');
// function no_category_base_deactivate() {
//     remove_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
//     // We don't want to insert our custom rules again
//     no_category_base_refresh_rules();
// }
// Remove category base
add_action('init', 'no_category_base_permastruct');
function no_category_base_permastruct() {
    global $wp_rewrite, $wp_version;
    if (version_compare($wp_version, '3.4', '<')) {
        // For pre-3.4 support
        $wp_rewrite -> extra_permastructs['category'][0] = '%category%';
    } else {
        $wp_rewrite -> extra_permastructs['category']['struct'] = '%category%';
    }
}
// Add our custom category rewrite rules
add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
function no_category_base_rewrite_rules($category_rewrite) {
    //var_dump($category_rewrite); // For Debugging
    $category_rewrite = array();
    $categories = get_categories(array('hide_empty' => false));
    foreach ($categories as $category) {
        $category_nicename = $category -> slug;
        if ($category -> parent == $category -> cat_ID)// recursive recursion
            $category -> parent = 0;
        elseif ($category -> parent != 0)
            $category_nicename = get_category_parents($category -> parent, false, '/', true) . $category_nicename;
        $category_rewrite['(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
        $category_rewrite['(' . $category_nicename . ')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
        $category_rewrite['(' . $category_nicename . ')/?$'] = 'index.php?category_name=$matches[1]';
    }
    // Redirect support from Old Category Base
    global $wp_rewrite;
    $old_category_base = get_option('category_base') ? get_option('category_base') : 'category';
    $old_category_base = trim($old_category_base, '/');
    $category_rewrite[$old_category_base . '/(.*)$'] = 'index.php?category_redirect=$matches[1]';
   
    //var_dump($category_rewrite); // For Debugging
    return $category_rewrite;
}
   
// Add 'category_redirect' query variable
add_filter('query_vars', 'no_category_base_query_vars');
function no_category_base_query_vars($public_query_vars) {
    $public_query_vars[] = 'category_redirect';
    return $public_query_vars;
}
   
// Redirect if 'category_redirect' is set
add_filter('request', 'no_category_base_request');
function no_category_base_request($query_vars) {
    //print_r($query_vars); // For Debugging
    if (isset($query_vars['category_redirect'])) {
        $catlink = trailingslashit(get_option('home')) . user_trailingslashit($query_vars['category_redirect'], 'category');
        status_header(301);
        header("Location: $catlink");
        exit();
    }
    return $query_vars;
}

加 好代码后到后台再次固定链接即可。现在标签(Category)页面的链接就变成了以下这种结构:“http://域名/分类/“也就是说成功的去除了链接中的 category

上面关于网站网址结构对于seo优化的话说了不少了这里就不多说了。

刚才是去掉了分类页面的category  在wordpress里面还有一个标签在输出网址的时候也添加了一个tag 所以这里说的是去掉tag

在functions.php里面添加以下代码:

register_activation_hook(__FILE__,'no_tag_base_refresh_rules');
add_action('created_post_tag','no_tag_base_refresh_rules');
add_action('edited_post_tag','no_tag_base_refresh_rules');
add_action('delete_post_tag','no_tag_base_refresh_rules');
function no_tag_base_refresh_rules() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
register_deactivation_hook(__FILE__,'no_tag_base_deactivate');
function no_tag_base_deactivate() {
    remove_filter('tag_rewrite_rules', 'no_tag_base_rewrite_rules');
    no_tag_base_refresh_rules();
}
add_action('init', 'no_tag_base_permastruct');
function no_tag_base_permastruct() {
    global $wp_rewrite, $wp_version;
    if (version_compare($wp_version, '3.4', '<')) {
        $wp_rewrite -> extra_permastructs['post_tag'][0] = '%post_tag%';
    } else {
        $wp_rewrite -> extra_permastructs['post_tag']['struct'] = '%post_tag%';
    }
}
add_filter('tag_rewrite_rules', 'no_tag_base_rewrite_rules');
function no_tag_base_rewrite_rules($tag_rewrite) {
    $tag_rewrite=array();
    $tags=get_tags(array('hide_empty'=>false));
    foreach($tags as $tag) {
        $tag_nicename = $tag->slug;
        if ( $tag->parent == $tag_id ) {
           $tag->parent = 0;
        }
        $tag_rewrite['('.$tag_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?tag=$matches[1]&feed=$matches[2]';
        $tag_rewrite['('.$tag_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?tag=$matches[1]&paged=$matches[2]';
        $tag_rewrite['('.$tag_nicename.')/?$'] = 'index.php?tag=$matches[1]';
    }
    global $wp_rewrite;
    $old_tag_base = get_option('tag_base') ? get_option('tag_base') : 'tag';
    $old_tag_base = trim($old_tag_base, '/');
    $tag_rewrite[$old_tag_base . '/(.*)$'] = 'index.php?tag_redirect=$matches[1]';
    return $tag_rewrite;
}
add_filter('query_vars', 'no_tag_base_query_vars');
function no_tag_base_query_vars($public_query_vars) {
    $public_query_vars[] = 'tag_redirect';
    return $public_query_vars;
}
add_filter('request', 'no_tag_base_request');
function no_tag_base_request($query_vars) {
    if (isset($query_vars['tag_redirect'])) {
        $tag = user_trailingslashit($query_vars['tag_redirect'], 'post_tag');
        $taglink = trailingslashit(get_option( 'home' )) . $tag;
        status_header(301);
        header("Location: $taglink");
        exit();
    }
    return $query_vars;
}

加上以上代码标签页就变成“http://域名/标签/ “简单的说就是去掉tag

但是现在还有一个问题就是对于搜索引擎,一个页面就是后面没有“/“   后面有/才是一个目录

那么如何让分类目录的链接变成“http://www.域名.com/分类名/“ 那么如何让标签目录的链接变成“http://www.域名.com/标签名/“

在functions.php里面添加以下代码切记一定要放在最后不然出问题就不好了

if (is_admin()) return;
$permalink_structure = get_option('permalink_structure');
if (!$permalink_structure || '/' === substr($permalink_structure, -1))
return;
add_filter('user_trailingslashit', 'ppm_fixe_trailingslash', 10, 2);
function ppm_fixe_trailingslash($url, $type)
{
if ('single' === $type)
return $url;
return trailingslashit($url);
}

这样就好了,对于网站网址结构的SEO优化!

发表回复