Remove Category Base From URL Without Plugin in WordPress

Remove Category Base Without Plugin to make your WordPress category URLs short and clean. Here’s how to do it with a simple functions.php tweak.

Step 1: Add the Category Base Removal Code

Add this to your child theme’s functions.php:


// Remove category base from URLs
add_filter(‘category_rewrite_rules’, ‘qdt_remove_category_base’);
function qdt_remove_category_base($category_rewrite) {
  $category_rewrite = array();
  $categories = get_categories(array(‘hide_empty’ => false));
  foreach ($categories as $category) {
    $category_nicename = $category->slug;
    if ($category->parent == $category->cat_ID) {
      $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]’;
  }
  return $category_rewrite;
}

add_filter(‘query_vars’, ‘qdt_category_query_vars’);
function qdt_category_query_vars($public_query_vars) {
  $public_query_vars[] = ‘category_redirect’;
  return $public_query_vars;
}

add_action(‘template_redirect’, ‘qdt_category_template_redirect’);
function qdt_category_template_redirect() {
  $cat = get_query_var(‘category_redirect’);
  if ($cat != ”) {
    $catlink = trailingslashit(get_option(‘home’)) . user_trailingslashit($cat, ‘category’);
    wp_redirect($catlink, 301);
    exit();
  }
}

How It Works

This snippet rewrites WordPress category permalinks to drop the /category/ base and redirects old URLs to the new format.

Why Remove Category Base Without Plugin?

Shorter, cleaner URLs are better for SEO and easier for visitors to remember and share.

Common Mistake

Always flush permalinks in Settings > Permalinks after adding this code to avoid 404 errors.

Pro Tip

Back up your .htaccess file and test your category URLs after saving.

Related Snippet

If you liked this, check out my guide on adding a custom favicon without a plugin in WordPress.

Leave a Comment