Skip to content
Advertisement

How to do 301 redirections after WordPress Permalinks change

I changed the Woocommerce permalink structure of my site and I would need some 301 redirections Help.

Woocommerce product permalinks were configured according to the structure:

/shop/%product_cat%/

And now according to the structure:

/shop/

What lines do I need to add to the .htaccess file to do 301 redirections, please?

UPDATE: /shop/%product_cat%/ is a Permalink Structure in WordPress that means example.com/shop/category1/subcategory1/MyProduct and /shop/ means example.com/shop/MyProduct and this latter is better for SEO.

Advertisement

Answer

/shop/%product_cat%/ is a Permalink Structure in WordPress an means example.com/shop/category1/subcategory1/MyProduct and /shop/ means example.com/shop/MyProduct

If the old URLs always contained two additional path segments of the form /category1/subcategory1 followed by a single path segment containing MyProduct (product-name) then you would need to do something like the following at the top of your .htaccess file before the WordPress front-controller:

RewriteRule ^shop/[^/]+/[^/]+/([^/]+)$ /shop/$1 [R=302,L]

It is always better to be more specific with the regex in order to avoid conflicts.

However, if you need a more general solution that would match single category path segments or more than two then use something like the following instead:

RewriteRule ^shop/(?:[^/]+/)+([^/]+)$ /shop/$1 [R=302,L]

The above would match any of the following URL formats and redirect them all to /shop/MyProduct:

  • /shop/category1/MyProduct
  • /shop/category1/subcategory1/MyProduct
  • /shop/category1/subcategory1/subsubcategory1/MyProduct
  • /shop/category1/subcategory1/subsubcategory1/etc/MyProduct

Test with 302 (temporary) redirects to avoid caching issues and only change to a 301 (permanent) redirect (if that is the intention) when you have confirmed it works OK.


UPDATE: myproduct page: https://www.example.com/shop/crazy-cat-test-t-shirt/ – my old product page (404 error): https://www.example.com/shop/women/t-shirt/crazy-cat-test-t-shirt/ – test adress with one category (Works): https://www.example.com/shop/category/crazy-cat-test-t-shirt/

This highlights “the problem” (as mentioned briefly in a preceding comment)…. All these URLs (old and new) contain a trailing slash. None of your previous “example” URLs contained a trailing slash?! The RewriteRule directive above will not work if the requested URL contains a trailing slash because it is designed to work with the examples you posted (without a trailing slash). URLs with and without a trailing slash are two completely different URLs.

If it’s currently “working” with single-category URLs then “something else” must be doing that or changing the request so the above directive matches. However, the above directive would omit the trailing slash from the (new) target, so “something else” would have needed to append the trailing slash, if that is a requirement. (?!)

If the trailing slash is always present on the “old” URL and a trailing slash is required on the “new” URL then change the above directive to read:

RewriteRule ^shop/(?:[^/]+/)+([^/]+/)$ /shop/$1 [R=302,L]

In the above, the mandatory trailing slash is included as part of the captured subgroup and therefore present in the $1 backreference.

However, if the trailing slash is optional on the “old” URL but should always be present on the “new” URL then change the directive to read:

RewriteRule ^shop/(?:[^/]+/)+([^/]+)/?$ /shop/$1/ [R=302,L]

In the above, the optional trailing slash is outside the captured group, so we must manually include this on the target URL, where it is mandatory.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement