WooCommerce: Redirect Empty Paginated Category Pages (404)

When managing a WooCommerce store, it’s common to rearrange products or remove them from certain categories. However, this can lead to an unexpected issue: paginated product category URLs (e.g., /product-category/shirts/page/3/) may still be accessible even when that specific page no longer contains any products.

Instead of displaying a 404 error, it’s often better to redirect users back to the main category page to avoid confusion and improve user experience.

This is especially important for SEO, as you don’t want search engines indexing irrelevant or broken pages. The solution is to detect when a paginated product category archive is empty or invalid and redirect to the base category URL instead.

Below is a simple snippet you can add to your theme’s functions.php or a custom plugin. It handles the redirect only when necessary, ensuring users and bots land on meaningful content.

A 404 error caused by an empty paginated WooCommerce product category page. Let’s redirect this monster!

PHP Snippet: Redirect Empty, Paginated, Category Pages to Main WooCommerce Product Category Page

This snippet hooks into the template_redirect action to catch 404 pages that resemble paginated product category URLs.

It first retrieves the WooCommerce product category base slug from the permalink settings, making it compatible with custom or translated slugs.

Then, it checks if the current request matches the pattern of a paginated category page. If so, it extracts the category slug from the URL, confirms the category exists, and safely redirects the user to the main category page.

This prevents visitors and search engines from encountering empty or broken paginated URLs after products have been removed from that category.

/**
 * @snippet       Redirect Empty Paginated WooCommerce Category Pages
 * @tutorial      https://businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

add_action( 'template_redirect', 'bbloomer_redirect_empty_product_cat_pagination' );

function bbloomer_redirect_empty_product_cat_pagination() {
	if ( is_404() ) {
		$requested_url = $_SERVER['REQUEST_URI'];
		$permalinks = wc_get_permalink_structure();		
		if ( preg_match( '#^/' . $permalinks['category_rewrite_slug'] . '/[^/]+/page/\d+/?#', $requested_url ) ) {
			preg_match( '#/' . $permalinks['category_rewrite_slug'] . '/([^/]+)/#', $requested_url, $matches );
			if ( isset( $matches[1] ) ) {
				$term = get_term_by( 'slug', $matches[1], 'product_cat' );
				if ( $term && ! is_wp_error( $term ) ) {
					wp_safe_redirect( get_term_link( $term ) );
					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

2 thoughts on “WooCommerce: Redirect Empty Paginated Category Pages (404)

  1. Hi, thanks for the snippet. Just wondering—would it be better to use a 301 redirect instead of 302 for SEO? Also, could this cause soft 404 issues if paginated category URLs were already indexed?

    Should we also set canonical URLs to the base category to avoid duplication?

    1. Hello there!

      – 301 is better if you plan to NEVER use the paginated category page again, 302 is better if it’s temporary
      – the 404 issue is exactly why we’re redirecting, so we avoid sending people from indexed URLs to 404
      – honestly, I’m not an SEO so I can’t answer the last question, I’d say Woo should already do that out of the box if I were Woo

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 *