Imagine if you could access your WooCommerce product page using a shorter and more engaging URL like example.com/sku00001 instead of the lengthy permalink example.com/shop/t-shirts/t-shirt-casual-v-neck.
In this snippet, we’ll create a simple redirect that maps a custom URL containing the product SKU to the corresponding product page. This way, you can use a concise and attractive URL in your email marketing, display advertising, blog posts, and anywhere you want to share a “pretty link” instead of a lengthy product page URL.
One thing worth flagging: this hook fires on every 404 page, not just SKU-shaped URLs, so it’s worth limiting the check to single-segment requests (no slashes) to avoid running a product lookup on every broken link across your site. The version below adds that guard.
function fixelar_redirect_sku_in_url_to_product() {
if ( ! is_404() ) return;
$sku = trim( $GLOBALS['wp']->request, '/' );
// Only treat single-segment requests as potential SKUs (no slashes)
if ( empty( $sku ) || strpos( $sku, '/' ) !== false ) return;
$id = wc_get_product_id_by_sku( $sku );
if ( ! $id ) return;
wp_safe_redirect( get_permalink( $id ), 301 );
exit;
}
add_action( 'template_redirect', 'fixelar_redirect_sku_in_url_to_product' );
Enjoy simplifying your links!