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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @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

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

10 thoughts on “WooCommerce: Redirect Product Category Pages

  1. On my Woocommerce store, when I click on a particular product category [Medical Consumables), it redirects to a blog category [Opinion].

    This particular action only applies to this category. How can I fix this please?

    Thank you.

    1. Do they have the same slug?

  2. Hi Rodolfo and guests.
    I just added one of the scripts above to my new website. I made a slight adjustment and it now works for every category as long as you create a new page with the same name as the category. Can anyone see any issues with this and does a redirect affect SEO? Ta.

    1. Hi there, if it works for your users, it also works for Google!

  3. Hello there! Thanks for this!!

    It happens I need to do the samething for a custom taxonomy I created for products as well.

    I was able to get it working for product categories even with custom slugs, applying the following modifications:

    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 ? '/catalogo/?_categoria=' . $term->slug : wc_get_page_permalink( 'shop' );   
             wp_safe_redirect( $url );
             exit;
       }
    }
    

    But couldn’t do the same for a custom taxonomy (“ambient” for example).
    Would you happen to know how could I do so, what paremeters I nedd to change or what’s the structure logic for custom taxonomies?

    Thanks in advance!

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

  5. 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 *