April 29, 2019

How to Remove the “/product” Prefix from WooCommerce URLs

Inspired by various SEO tutorials, some WordPress users aim to keep their URLs as short as possible — for example, by removing the category base or eliminating the /product prefix from product URLs. Whether this really has a significant impact on SEO is debatable, but today, let’s focus on the technical part: how to remove the “/product” prefix from WooCommerce URLs.

Option 1: Use the Permalink Manager for WooCommerce Plugin

The easiest and most reliable way to remove the /product prefix is by using the Permalink Manager for WooCommerce plugin. This plugin allows you to fully customize the permalink structure of your WooCommerce store — including the removal of slugs like product, product_category, and product_tag.

🔗 Plugin Link: Permalink Manager for WooCommerce

Pros:

  • Simple setup with flexible URL customization.

Cons:

  • Adds an extra plugin, which might have a slight impact on site performance.

Option 2: Use Custom Rewrite Rules with Code

If you’re comfortable with coding, you can remove the /product slug by adding some custom code to your theme’s functions.php file.
Keep in mind that modifying permalink rules can potentially cause conflicts with other pages, posts, or custom post types, so proceed with caution and test thoroughly.

Here’s a sample snippet to remove the /product prefix:

add_filter('register_post_type_args', 'wprs_remove_product_slug', 10, 2);
function wprs_remove_product_slug($args, $post_type) {
    if ($post_type === 'product') {
        $args['rewrite']['slug'] = '';
    }
    return $args;
}

function wprs_product_rewrite_rule() {
    add_rewrite_rule('^([^/]+)(/[0-9]+)?/?$', 'index.php?product=$matches[1]', 'top');
}
add_action('init', 'wprs_product_rewrite_rule');

Pros:

  • No additional plugin required.

Cons:

  • Requires manual permalink flushing and careful handling of rewrite rules to avoid conflicts.

Important Considerations

SEO & Redirects:
If your site has been using the default /product URLs for a while, removing the slug may affect SEO. Make sure to implement proper 301 redirects from the old URLs to the new ones to avoid broken links.

URL Conflicts:
Removing the /product slug can lead to conflicts with other pages or post types. Test your site thoroughly to make sure everything works smoothly after the change.