SEO观察
研究搜索引擎优化(SEO)技术。

去掉WordPress分类URL链接中 category 的五种方法

WordPress程序建站默认会在URL链接中自带category字符,致使WordPress中的URL链接层级加深,不仅会影响搜索引擎收录网页,还会影响网页权重的传递和关键词排名,同时影响URL的美观度。

去掉 WordPress 分类目录 URL链接中 category 的五种方法1

为了WordPress中URL的规范化与美观度,也为了搜索引擎能更快收录网页,茹莱神兽建议,可以通过以下五种方法去除“category”前缀。

一、安装使用WordPress插件去除“cotegory”

WordPress插件功能非常强大,安装No category parents 和 WP No Category Base 插件就能去掉URL中的“cotegory”字符,两个插件可以任选其一进行使用。

二、添加使用代码去除“cotegory”

如果站长认为安装WordPress插件会影响网站的加载速度,那么,茹莱神兽认为可以添加使用代码去掉“cotegory”字符,只需要把以下三种代码中的其中一种添加在 functions.php 保存即可。

代码一:

add_filter( 'category_link', 'zm_category_link', 10, 2 );
function zm_category_link( $catlink, $category_id ) {
	global $wp_rewrite;
	$catlink = $wp_rewrite->get_category_permastruct();
 
	if ( empty( $catlink ) ) {
		$catlink = home_url('?cat=' . $category_id);
	} else {
		$category = &get_category( $category_id );
		$category_nicename = $category->slug;
 
		$catlink = str_replace( '%category%', $category_nicename, $catlink );
		$catlink = home_url( user_trailingslashit( $catlink, 'category' ) );
	}
	return $catlink;
}

说明:上传新的 function.php 之后,记得将“固定链接”的设置保存一次才会生效,这个方法会导致分类链接中的 category 同时都能访问,没有做 301 跳转,所以还是建议大家使用上面的插件去除分类链接的 category。

代码二:

/*  
*WordPress去除category
*/
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;
}

代码三:

// Remove category
function remove_category( $string, $type )  {
    if ( $type != 'single' && $type == 'category' && ( strpos( $string, 'category' ) !== false ) ){ 
        $url_without_category = str_replace( "/category/", "/", $string );
        return trailingslashit( $url_without_category ); 
    }
    return $string;  
}     
add_filter( 'user_trailingslashit', 'remove_category', 100, 2);

在functions.php添加代码去除“cotegory”字符,确实比安装WordPress插件要好一些,因为WordPress不能安装过多插件,否则会影响网站的加载速度和用户体验。

三、修改WordPress函数去除“cotegory”

找到你Wordpress博客wp-includes文件夹下的category-template.php。然后打开此文件,搜索(get_category_link()函数里)的以下代码:

$catlink = $wp_rewrite->get_category_permastruct();

在其后,另起一行加入:

$catlink = str_replace('/category' , '' , $catlink);

然后保存上传到服务器即可。

代码作用是,使用字符串替换函数,把获取分类链接函数的分类链接里的/category替换掉。达到去掉分类链接前缀/category的目的。

四、设置分类目录前缀可去除“cotegory”

另外 WordPress 还有一个非常方便的方法,可以说是最简化去掉分类目录 URL 中的 category。

去掉 WordPress 分类目录 URL链接中 category 的五种方法

只需要在 WordPress 后台,点击「设置」菜单下的「固定链接」子菜单,将「分类目录前缀」设置为英文符号点( .) ,然后保存即可,如上图。

五、修改网站根目录中的 .htaccess 文件

如果以上四种方法不合适,站长也可以通过修改网站根目录下的 .htaccess 文件来实现,把以下规则代码添加到网站根目录下的 .htaccess文件中:

RewriteRule ^category/(.+)$ http://www.badpon.com/$1 [R=301,L]

需要注意的是,把 badpon.com 换成你自己的网址即可。这种方法需要一定的技术含量,毕竟与规则有关,稍不注意就会影响网站,也有可能会导致网站无法打开,所需需要谨慎。

本篇最后总结

茹莱神兽认为,以上五种方法都可以去掉父分类parent-category字符,SEO站长可以选择其中一种方法即可。去掉category后,WordPress的URL更加规范完美,网站的结构对搜索引擎更加友好。

本文地址: https://www.badpon.com/6135.html,作者茹莱神兽,转载请注明出处。

赞(0) 打赏
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
免责申明:本站部分内容、图片来源于网络,版权归原作者所有,如有侵权,请联系底部QQ邮箱,本站将及时删除,谢谢!

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

微信扫一扫打赏

error: Content is protected !!