WooCommerce: Redirect Product Category Pages

Maybe because you have only one product category and therefore search engines would find duplicate content (Shop page = Category page) and penalize your website.

Or maybe because you use advanced product filters and you prefer customers to see the filtered view “by category” (e.g. “example.com/shop/?_product_category=tables“) as opposed to the default category pages ( “example.com/product_category/tables” ).

Either way, it is possible to programmatically redirect all product category pages to a given page or to a relevant URL with parameters – and here’s the fix. Enjoy!

Product categories come with their own page. But sometimes you may want to unindex + redirect these pages for SEO reasons. Here are two solutions to redirect all pages to the Shop page, or to a category-specific URL with parameters

PHP Snippet 1: Redirect All Product Category Pages to Shop Page

/**
 * @snippet       Redirect To Shop @ WooCommerce Category Pages
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/
 */

add_action( 'wp', 'bbloomer_redirect_cats_to_shop' );

function bbloomer_redirect_cats_to_shop() {
	if ( is_product_category() ) {
      wp_safe_redirect( wc_get_page_permalink( 'shop' ) );
      exit;
   }
}

PHP Snippet 2: Redirect Each Product Category Page to A Relevant URL With Parameters

/**
 * @snippet       Redirect To URL + Parameters @ WooCommerce Category Pages
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/
 */

add_action( 'wp', 'bbloomer_redirect_cat_to_relevant_filter' );

function bbloomer_redirect_cat_to_relevant_filter() {
	if ( is_product_category() ) {
		$term = get_queried_object();
		$url = $term && $term->slug ? '/shop/?_product_category=' . $term->slug : wc_get_page_permalink( 'shop' );	
         wp_safe_redirect( $url );
         exit;
   }
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Custom Add to Cart URLs – The Ultimate Guide
    In WooCommerce you can add a product to the cart via a custom link. You just need to use the “add-to-cart” URL parameter followed by the product ID. This tutorial will show you how to create custom URLs to add simple, variable and grouped products to the cart – as well as defining the add […]
  • WooCommerce: Add Second Description @ Product Category Pages
    In terms of SEO, if you’re trying to rank your product category pages, you really need to make the most of the default WooCommerce product category “description” and “thumbnail”. Most themes, if compatible with WooCommerce, will show this content right below the product category name and above products. Nothing new so far. But what if […]
  • WooCommerce: Show Category For Each Product @ Shop Page
    A client of mine has a category called “Brand”. Each product is assigned to a specific Brand subcategory e.g. Nike, Adidas, etc. The goal of this task was to show the “Brand” subcategories in the shop/category/loop pages as a way to help the user identify the brand name. The same can be applied to showing […]
  • WooCommerce: Should I Noindex Product Tag Pages?
    Ah, yet another million dollar question. If you have organized your WooCommerce products properly, you have “N” product categories and “M” product tags, where “M > N” (actually, if there were such a thing in algebra, it should be “M >>> N” as each product should be assigned to 1 category and multiple tags). This […]
  • WooCommerce: Change Product Permalinks @ Shop / Loop Pages
    A BloomerArmada fan asked me a very interesting question: how can I link each product in the shop page to its own custom landing page as opposed to the default permalink? Of course this applies when you don’t want to use the default single product page for all or some products. Clearly, you could set […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

4 thoughts on “WooCommerce: Redirect Product Category Pages

  1. Thank you! Works great!
    How do i redirect it to the category on the shop page. now it only redircts to the shop page.

  2. Great snippet.

    I would suggest using

    !is_admin()

    in the conditional check. Otherwise when using a category filter on the back-end for products, it will redirect you to the front-end shop page.

    E.g.

    if (is_product_category() && !is_admin()) {
                $term = get_queried_object();
                $url  = $term && $term->slug ? '/shop/?_product_category=' . $term->slug : wc_get_page_permalink('shop');
                wp_safe_redirect($url);
                exit;
            }
    
Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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