Subcategory Redirect Woo
301 redirect short Woo subcategory URL to full canonical URL with parent slug
by Torwald45 · github.com/torwald45/wp-snippet-subcategory-redirect-woo
★ 0stars
0forks
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://github.com/torwald45/wp-snippet-subcategory-redirect-woo/archive/refs/heads/main.zipFrom the readme
WP Snippet: Subcategory Redirect Woo
Woo allows accessing subcategory pages via short URL (without parent slug), creating duplicate URLs for the same content. This snippet detects short URLs and performs 301 redirect to the canonical full URL with parent slug.
Example
Short URL (duplicate):
/product-category/child-category/
Canonical URL (correct):
/product-category/parent-category/child-category/
The snippet redirects the short URL to the canonical one with 301 status code.
Requirements
- WP 5.0 or higher
- PHP 7.4 or higher
- Woo with hierarchical product categories
Installation
Method 1: functions.php
1. Open your theme's functions.php file
2. Copy the entire content from subcategory-redirect-woo.php
3. Paste at the end of your functions.php
4. Save the file
Method 2: Code Snippets Plugin
1. Install and activate the Code Snippets plugin
2. Go to Snippets Add New
3. Copy content from subcategory-redirect-woo.php WITHOUT the opening <?php tag
4. Set scope to "Only run on the front end"
5. Activate the snippet
Technical Details
Hooks Used
- templateredirect (detect and redirect short subcategory URLs)
How It Works
1. Checks if current page is a product category taxo