WooCommerce: Redirect URL With SKU To Single Product Page

Let’s say your WooCommerce product has the following permalink: example.com/shop/t-shirts-tops-and-blouses/t-shirt-casual-v-neck-longsleeve-green-medium-cotton-stretch

Wouldn’t it be shorter, more fun and neater if you could also reach the same exact single product page by using example.com/sku00001 in the browser bar, where “sku00001” is the product SKU?

Well, in today’s snippet we will create a simple redirect from a custom URL that contains the product SKU to the product page, so that you can use the URL “shortener in your email marketing, display advertising, blog posts and anywhere you want to show a “pretty link” instead of a full, long, boring product page URL.


If I enter example.com/SKU I want to be redirected to the single product page. Let’s code this!

PHP Snippet: Redirect “example.com/sku” To The WooCommerce Single Product Page

 * @snippet       Redirect SKU in URL to Woo Product Page
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     Join https://businessbloomer.com/club/

add_action( 'template_redirect', 'bbloomer_redirect_sku_in_url_to_product' );

function bbloomer_redirect_sku_in_url_to_product() {
	if ( is_404() && ( $sku = $GLOBALS['wp']->request ) && ( $id = wc_get_product_id_by_sku( $sku ) ) ) {
		wp_redirect( get_permalink( $id ) );
  • The first line hooks the function bbloomer_redirect_sku_in_url_to_product to the template_redirect action. This means the function will be called whenever a WordPress template is redirected, which is typically on every page load.
  • The bbloomer_redirect_sku_in_url_to_product function itself is commented to explain its purpose, which is to redirect URLs containing a Stock Keeping Unit (SKU) to the corresponding product page.
  • Inside the function, we first check if the current page is a 404 error page using the is_404() function. This is because we only want to redirect URLs that result in a 404 error, which might be the case if someone types in a product SKU directly in the browser address bar.
  • If it is a 404 page, we then try to retrieve the request string from the global $wp object.
  • We then use the WooCommerce function wc_get_product_id_by_sku($sku) to try to get the product ID for the SKU in the request string.
  • If a product ID is found for the SKU, we use the WordPress function wp_redirect(get_permalink($id)) to redirect the user to the product’s permalink. The get_permalink($id) function retrieves the permanent URL for the product with the given ID.
  • Finally, we use the exit function to stop the function from executing any further code after the redirect.

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: Hide Category & Tag @ Single Product Page
    SKU, Category list and Tag list on the WooCommerce single product page are called “product meta”. We already saw how to hide just the SKU (while leaving Cats and Tags there), so in this “episode” we will study how to do the opposite i.e. keeping the SKU there while hiding Cats and/or Tags. If you […]
  • WooCommerce: Hide SKU @ Single Product Page
    This is a very common task. As a WooCommerce store manager, sometimes you need to hide the SKU field on the single product page, while keeping it in the backend (Product Edit page) for order tracking and product import/export purposes. Here’s a simple snippet you can use to remove the SKU immediately 🙂
  • WooCommerce: Show SKU @ Cart, Checkout, Order & Emails
    When SKU matters to the end user, displaying it in the Cart page, Checkout page, Thank you page, My Account View Order page and Order Emails under the item name is a must. Ideal for B2B businesses and international brands, this simple customization can help you learn how to add any sort of content under […]
  • WooCommerce: Login Redirect by User Role @ My Account
    There are times when you don’t want customers to login and be redirected to the default “My Account” dashboard. Maybe because you have a membership site and you want them to go to a custom “Welcome” page, or maybe you wish to send them straight to their “My Account” > “Downloads” subsection. No matter the […]

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

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 *