WooCommerce: How to Remove Product Category from URL (SEO-Friendly Guide)
Introduction:
Having a clean and concise URL structure is crucial for any e-commerce website. It not only enhances user experience but also contributes significantly to your Search Engine Optimization (SEO) efforts. By default, WooCommerce includes the product category in the URL, like so: `yourdomain.com/product-category/category-name/product-name/`. While functional, this can sometimes be considered bulky and less appealing for both users and search engines. This article will guide you through the process of removing the “product-category” base from your WooCommerce product URLs, boosting your website’s SEO and overall aesthetic. We’ll explore different methods, weighing their pros and cons, so you can choose the best approach for your specific needs.
Main Part: Removing the “product-category” Base
Why Remove “product-category” from the URL?
Before diving into the how-to, let’s understand why you might want to remove this segment from your URLs:
- Improved SEO: Shorter URLs are often perceived as more user-friendly and easier to share, potentially leading to higher click-through rates and improved search engine rankings.
- Better User Experience: Cleaner URLs are easier to remember, type, and share, contributing to a better browsing experience.
- Brand Consistency: Shorter and more personalized URLs can better align with your brand’s image and marketing strategies.
- Installation and Activation: Install and activate the Yoast SEO plugin from the WordPress plugin repository.
- Permalink Settings: Navigate to Yoast SEO > Search Appearance > Taxonomies.
- Remove the Product Category Slug: Find the “Product category URL” setting and select “Remove”.
- Save Changes: Save your changes.
- Easy to use: User-friendly interface and simple configuration.
- No code required: Perfect for non-technical users.
- SEO benefits: Yoast SEO offers many other features that can improve your website’s SEO.
- Requires the Yoast SEO plugin.
Methods for Removing “product-category”
Here are a few methods you can use to achieve this:
1. Using the Yoast SEO Plugin (Recommended):
Yoast SEO is a powerful and popular SEO plugin for WordPress. It offers a straightforward way to remove the “product-category” base.
Pros:
Cons:
2. Using Custom Code (Advanced):
If you prefer a code-based approach or want to avoid using a plugin, you can add custom code to your theme’s `functions.php` file or a custom plugin. Important: Back up your website before making any code changes!
function custom_remove_product_category_slug( $permalink, $post ) { if ( $post->post_type != 'product' ) return $permalink;
$terms = get_the_terms( $post->ID, ‘product_cat’ );
if ( empty( $terms ) ) {
return str_replace( ‘/product-category/’, ‘/’, $permalink );
}
$category_slug = reset( $terms )->slug;
return str_replace( ‘/product-category/’ . $category_slug . ‘/’, ‘/’, $permalink );
}
add_filter( ‘post_type_link’, ‘custom_remove_product_category_slug’, 10, 2 );
function custom_product_category_rewrite_rules( $wp_rewrite ) {
$new_rules = array();
$new_rules[‘([^/]+)/?$’] = ‘index.php?product=$matches[1]’;
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}
add_filter( ‘rewrite_rules_array’, ‘custom_product_category_rewrite_rules’ );
function custom_product_category_flush_rewrite_rules() {
flush_rewrite_rules();
}
add_action( ‘init’, ‘custom_product_category_flush_rewrite_rules’ );
Explanation:
- The first function `custom_remove_product_category_slug` removes the `product-category` from the URL.
- The second function `custom_product_category_rewrite_rules` adds rewrite rules to WordPress to handle the modified URLs.
- The third function `custom_product_category_flush_rewrite_rules` flushes the rewrite rules to apply the changes (this only needs to run once).
Important steps after adding the code:
- Save the `functions.php` file.
- Go to WordPress Admin > Settings > Permalinks and click ‘Save Changes’ (without changing any settings). This action flushes the rewrite rules.
Pros:
- No plugin required: Reduces the number of plugins on your website.
- More control: Allows for greater customization.
Cons:
- Requires coding knowledge: Not suitable for beginners.
- Potential for errors: Mistakes in the code can break your website.
3. Using a Dedicated Plugin (e.g., Custom Permalinks):
Several plugins are designed specifically for customizing permalinks, including removing the “product-category” base. Search the WordPress plugin repository for “Custom Permalinks” and choose a reputable option with good reviews.
Pros:
- Simplified customization: Often provides a user-friendly interface for managing permalinks.
Cons:
- Plugin dependency: Relies on a third-party plugin.
Important Considerations:
- 301 Redirects: If you already have indexed URLs with the “product-category” in them, it’s crucial to set up 301 redirects from the old URLs to the new ones. This will prevent broken links and ensure that search engines transfer the link equity from the old URLs to the new ones. The Yoast SEO plugin can help with this as well.
- Backup Your Website: Always back up your website before making any changes to the code or permalinks. This will allow you to quickly restore your website if something goes wrong.
- Test Thoroughly: After implementing any of these methods, test your website thoroughly to ensure that all links are working correctly and that there are no broken pages.
- Re-submit Sitemap: Resubmit your website sitemap to search engines like Google so they crawl your website and understand the URL structure changes.
Conclusion:
Removing the “product-category” from your WooCommerce URLs can be a beneficial step towards improving your website’s SEO and user experience. Choose the method that best suits your technical skills and requirements. Whether you opt for the simplicity of the Yoast SEO plugin, the flexibility of custom code, or the convenience of a dedicated permalink plugin, remember to back up your website, test thoroughly, and implement 301 redirects to avoid losing any SEO value. By following these guidelines, you can create cleaner, more SEO-friendly URLs that contribute to the success of your WooCommerce store.