How to Remove /product/ Prefix from WooCommerce Product URLs
Influenced by some SEO tutorials, there are always some WordPress users who want to keep the URL hierarchy as short as possible, for exampleRemove the category prefixIf you want to remove the "product" prefix from product URLs, we will leave aside the question of whether or not this has significant SEO benefits. Today we will look at how to implement this feature in WooCommerce, removing the "product" prefix from URLs.
Option 1: Using the Permalink Manager Plugin
The easiest and most reliable way to remove the "product" prefix from a URL is to use the Permalink Manager for WooCommerce Plugin. This plugin allows you to customize the URL structure of WooCommerce and easily remove slugs such as "product", "product_category" and "product_tag".
Plug-in address::Permalink Manager for WooCommerce
- vantage: Simple setup and flexible URL customization.
- drawbacks: An additional plugin has been added which may affect performance slightly.
Option 2: Use Custom Code with Permalink Rewrite Rules
If you're familiar with the code, you can also get the code to work by adding a new code to the theme's functions.php file to remove the "product" prefix. Note that custom rewrite rules may have compatibility issues with page URLs or other plugins, so be careful when using them.
Sample code for removing 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');
- vantage: Implemented using the WordPress add_rewrite_rule function, no additional plugins required.
- drawbacks: Need to handle permalink refreshes and may need to troubleshoot using custom rules.
Important Notes
- Search Engine Optimization and Redirection: Removing the "product" prefix may affect SEO, especially if your site has been using the default URL structure for some time. To avoid broken links, make sure you do a 301 redirect of the old URL to the new one.
- Test Conflicts: Removing the "product" prefix may result in conflicts with other pages, articles, or custom post types. Please be sure to perform sufficient testing to ensure there are no other issues.
summarize
If you want to quickly and easily remove the "product" prefix from a URL, you can choose to use the Permalink Manager for WooCommerce Plugin. If you prefer to use code, this can be achieved through custom rewrite rules, but this may require additional troubleshooting work.
Lastly, we would like to remind you that although it is possible to make WooCommerce URLs more concise through technical means, in our experience, keeping the categorized directory or "product" prefix will not negatively affect the ranking when it comes to SEO. Therefore, you can decide whether or not to optimize the URL structure according to your needs.