wordpress优化:如何去掉category,优化分类目录URL层级

做seo的都知道,想要做好网站,url优化是至关重要的,所以我们希望网站url层级尽可能简短,而采用WordPress模板建站时,站长们都会将文章的固定链接设置成http://www.lulinling.com/%category%/%post_id%.html的形式,因为这种静态URL形式是遵循URL优化规则的。但是这种方法的缺点是,应用这种形式的固定链接以后,分类目录页的url多了一个category层级,此时我们的文章地址就变成了4层。为了简化URL层级做好网站优化,我们一定要去掉category目录,那么应该如何去掉分类目录链接中的category目录呢?

wordpress优化:如何去掉category,优化分类目录URL层级

根据小编的了解,应该有4种方法可以去除category分类标志,在这里小编把4种方法及其优缺点都列出来,供新手站长们参考学习。

1、根据wordpress后台更改固定链接设置

进入Wordpress后台→设置→固定链接:
根据wordpress后台更改固定链接设置
如上图,就是在“分类目录前缀”里输入半角字符“.”,点击保存更改后便可去掉分类目录前缀category。

优点:设置简单,特别适用于刚刚建成的新站或者页面数量少的网站。不用更改源代码,也不要安装插件。

缺点:这样设置会导致原链接打不开,网站出现大量404页面。已有良好收录基础或建了很多外链的网站需要慎重,不要因为操作简单而丢失大量外链以及权重。

2、利用Wordpress插件

“WP No Category Base”插件:

在wordpress后台插件→安装插件中搜索“WP No Category Base”(如下图),随后下载安装。此插件功能简单,主要是为了去除category目录标志而开发的一款wordpress插件,直接安装就可以应用,无需任何设置。
如何去掉category--利用插件
此插件安装启用后,需要对之前的分类URL做301跳转,以保留原有URL的权重传递。

“No category parents”和“Top Level Categories”插件也可以去除category分类,且安装与上述插件一样简单。其中“No category parents”是“WP No Category Base”插件的加强版,不仅可以去掉分类链接前缀category,还可以去掉父分类parent-category。

优点:安装简单。无论是初建成的新站,还是搜索引擎收录良好的网站,都可以应用,且不用更改代码。将原链接通过301跳转到新链接,最大程度保留网站权重,避免大量404错误链接导致网站被降权。

不足:插件或许会带来空间负担。因为带来的“空间负担”微乎其微,可以忽略不计,所以算不上缺点,介意的站长就不要应用。

3、更改Wordpress函数

找到Wordpress网站wp-includes文件夹下的category-template.php文件,然后打开这个文件,搜索(get_category_link()函数)的以下源代码:

$catlink = $wp_rewrite->get_category_permastruct();

在这行代码之后,另起一行添加如下代码:

$catlink = str_replace(‘/category’ , ” , $catlink);

这段代码的作用是:应用字符串替换函数,把获取分类链接函数的分类目录里的/category/替换掉。以实现去掉分类链接前缀/category/的目的。

最后将保存好的文件上传到服务器对应的文件夹内,覆盖原有文件。

优点:仅添加一行源代码,操作方便,性能上的影响可以忽略不计。适用于对源代码稍有了解的站长。

缺点:更改源代码,网站更新也要更改。且原链接仍可以访问,会给网站带来重复内容,需要对原链接做301跳转。

4、无插件纯代码实现

下面的代码就是 WP No Category Base 插件的,直接将以下代码复制到当前主题 Functions.php 中即可:

/*
Plugin Name: WP No Category Base
Plugin URI: http://blinger.org/wordpress-plugins/no-category-base/
Description: Removes ‘/category’ from your category permalinks.
Version: 1.1.1
Author: iDope
Author URI: http://efextra.com/
*/

// Refresh rules on activation/deactivation/category changes
register_activation_hook(__FILE__, ‘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;
}

// For Debugging
//add_filter(‘rewrite_rules_array’, ‘no_category_base_rewrite_rules_array’);
//function no_category_base_rewrite_rules_array($category_rewrite) {
// var_dump($category_rewrite); // For Debugging
//}

// 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;
}

特别注意:

不论是安装插件还是用源代码都有可能会出现404错误页面,因为网站原来设置的固定链接%post_id%.html的伪静态失效了。

解决方案:登入后台→设置→固定链接,随便改一下固定链接格式,随后再改成自个正常用的符合网站伪静态规则的链接格式,就能解决这个bug,不行的话就反复多改几次。

如果还是出现404,建议把清理缓存后再尝试一下!

目前在“wordpress优化:如何去掉category,优化分类目录URL层级”上有2条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注