How to Remove the Category Slug From WordPress Permalinks
By default, WordPress adds “/category/” to every category URL on your site.
This means your category links look longer than they need to, and the extra segment can affect how search engines read your URL structure.
In this post, I’ve shared a code snippet that removes the category slug from WordPress permalinks and keeps the clean path working for both top-level categories and sub-categories.
Site owners who care about clean, readable URLs and want category links that match the style of their post permalinks will find this most useful.
When to Use This
Add this snippet if any of these situations match your setup:
- Your post permalinks already use the category name, and you want category archive URLs to match the same pattern.
- You run a blog or content site where readable URLs matter for sharing and appearance.
- Your site has nested categories and you want sub-category URLs to reflect the full parent/child path without a “/category/” prefix.
- You want category feed and pagination URLs to work correctly after removing the base slug.
- You prefer a code-only fix rather than installing a plugin just to change a permalink setting.
When to Avoid This
Some setups conflict with custom rewrite rules, so check these before adding the snippet:
- Your site uses a caching plugin that stores old URLs. You’ll need to flush the cache after adding the snippet, or visitors may hit 404 errors.
- Another plugin on your site already handles category base removal. Running two rewrite solutions at the same time can break category URLs.
- Your post permalink structure does not include
%category%. The part of this snippet that adjusts post links only works when%category%is in your permalink setting. - Your theme or plugin generates category URLs directly without using WordPress filters. Those URLs won’t change because the snippet can’t intercept them.
- You have a large site with hundreds of categories. Custom rewrite rules for every category add overhead to the rewrite table and may slow down URL matching.
- You use a page or post with a slug that matches a category name. Removing the category base can cause WordPress to confuse the two and return the wrong content.
The Code Snippet
/* =========================================================
* Rewrite rules for categories & sub-categories
* ========================================================= */
function bloggerzia_category_rewrite_rules($rules) {
$new_rules = array();
// Get all categories (including empty ones)
$categories = get_categories(array(
'hide_empty' => false
));
foreach ($categories as $category) {
// Get the full hierarchical path (parent/child)
$category_parents = get_category_parents($category->term_id, false, '/', true);
$category_parents = rtrim($category_parents, '/'); // Remove trailing slash
// Add feed rule for this category
$new_rules[$category_parents . '/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] =
'index.php?category_name=' . $category->slug . '&feed=$matches[1]';
// Add pagination rule for this category
$new_rules[$category_parents . '/page/?([0-9]{1,})/?$'] =
'index.php?category_name=' . $category->slug . '&paged=$matches[1]';
// Add basic category rule (without /category/ base)
$new_rules[$category_parents . '/?$'] =
'index.php?category_name=' . $category->slug;
}
// Merge new rules with existing rules
return $new_rules + $rules;
}
add_filter('category_rewrite_rules', 'bloggerzia_category_rewrite_rules');
/* =========================================================
* Adjust post permalinks to include primary category
* ========================================================= */
function bloggerzia_post_link_category($permalink, $post) {
$categories = get_the_category($post->ID);
if (!empty($categories)) {
// Take the first (primary) category
$primary_category = $categories[0];
// Replace %category% in the permalink with primary category slug
$permalink = str_replace('%category%', $primary_category->slug, $permalink);
}
return $permalink;
}
add_filter('post_link', 'bloggerzia_post_link_category', 10, 2);
/* =========================================================
* Remove '/category/' base from category permalinks
* ========================================================= */
function bloggerzia_category_base_removal($link, $category) {
// Replace '/category/' with '/'
return preg_replace('@/category/@', '/', $link, 1);
}
add_filter('category_link', 'bloggerzia_category_base_removal', 10, 2);How to Add This Code Snippet
Follow these steps to add the snippet to your site:
- Add it to your child theme’s functions.php file or a code snippet plugin.
- Paste the code and save the changes.
- Go to Settings, then Permalinks, and click Save Changes to flush the rewrite rules. Do this even if you change nothing on that page.
- Test your category URLs to confirm the “/category/” segment no longer appears.
- Test on a staging site before using it on your live site.
Note: Use a child theme or a code snippet plugin. Avoid editing core files. Test on a staging site before going live. This snippet may not work with all themes or plugins.