how to remove category base from WordPress url

Removing the category base from WordPress URLs can greatly streamline your website’s URLs, making them more user-friendly and potentially improving your SEO performance. In WordPress, URLs including category bases typically appear as example.com/category/my-category/. Removing the ‘category’ segment leads to a cleaner URL, like example.com/my-category/. This guide outlines several methods to achieve this, catering to different levels of technical expertise.

Using a Plugin to Remove Category Base

The simplest approach for those uncomfortable with code is to use a plugin designed for this purpose. Plugins like Yoast SEO, Rank Math, and Remove Category URL can help achieve this with minimal hassle.

Yoast SEO

  1. Install Yoast SEO: Install and activate Yoast SEO from the WordPress plugin repository.
  2. Navigate to SEO Settings: Go to SEO > Search Appearance in your WordPress dashboard.
  3. Adjust the Taxonomies Tab: Under the ‘Taxonomies’ tab, find the ‘Category URLs’ section and toggle the ‘Remove the categories prefix’ option.
  4. Save Changes: Save your changes and Yoast SEO will automatically take care of removing the category base from your URLs.

Rank Math

  1. Install Rank Math: Install and activate Rank Math from the WordPress plugin repository.
  2. Access Rank Math Settings: Go to Rank Math > General Settings.
  3. Modify the Breadcrumbs Setting: Navigate to the ‘Breadcrumbs’ section and enable ‘Remove Category Base’.
  4. Save Changes: After saving, your category base will be removed from all category URLs.

Using .htaccess to Remove Category Base

For those comfortable editing system files, changes can be made directly in the .htaccess file, which controls the server’s directory and subdirectory requests.

  1. Access .htaccess: Connect to your site via FTP or cPanel, go to the root folder, and locate the .htaccess file.
  2. Back Up: Always back up your .htaccess file before making changes.
  3. Edit the File: Open your .htaccess file and add the following rewrite rule:
    RewriteRule ^category/(.+)$ http://example.com/$1 [R=301,L]

    Ensure you replace example.com with your actual domain name.

  4. Check Your Site: After saving the changes, check your website to ensure that all pages are loading correctly without any errors.

Editing WordPress Functions.php

A more technical method involves adding a snippet of code to your theme’s functions.php file:

  1. Open Functions.php: Access your theme’s functions.php file via FTP or the WordPress Theme Editor.
  2. Insert Code: Add the following code at the end of the file:
    add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
    function no_category_base_rewrite_rules($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)// recursive recursion
          $category->parent = 0;
        elseif ($category->parent != 0)
          $category_nicename = get_category_parents($category->parent, false, '/', true) . $category_nicename;
        $category_rewrite['(' . $category_nicename . ')/?$'] = 'index.php?category_name=$matches[1]';
      }
      return $category_rewrite;
    }
  3. Update Permalinks: Go to Settings > Permalinks and re-save the settings to flush the rewrite rules and apply your changes.
  4. Verify Functionality: Ensure that all links work properly without the category base.

Precautions & SEO Considerations

  • Breaking Changes: Always back up your website before making changes.
  • SEO Impact: While cleaner URLs can aid SEO, changing URL structures on an established site can temporarily affect rankings. Use 301 redirects to avoid losing SEO value.
  • Plugin Dependency: If using a plugin, remember that deactivating the plugin will likely revert the URLs back to including the category base.

By removing the category base from WordPress URLs, you create cleaner, more concise URLs that enhance user experience and can lead to better crawl efficiency from search engines. Choose the method that best fits your comfort level and resource availability, and always make sure to monitor your site’s performance post-implementation.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *