Working method for deleting product-category and product in WordPress

How to remove product-category and product from the URL and then get the correct 301 redirect.

1. Install the plugin
https://clubwp.ru/resources/woocommerce-perfect-seo-url.189/

In some cases (due to the specifics of some themes or plugin conflicts), Perfect Seo URl is not set correctly and the redirect does not work or does not work correctly. Old links have a status of 200, new ones have a status of 200 or 302. For search engines, we need a status of 301.

To get a 301 redirect in the function.php file add the following

Remove product-category from category URL:

function base_category_redirect(){
$link = $_SERVER[‘REQUEST_URI’];

if( strpos($link, ‘product-category/’)!==false ){
$new_link = get_bloginfo(‘siteurl’).str_replace(‘product-category/’, ”, $link);
Header( “HTTP/1.1 301 Moved Permanently” );
Header( “Location: $new_link” );
exit();
}
}
add_action(‘init’, ‘base_category_redirect’, -10);

Remove product from product URL:

function base_category_redirect2(){
$link = $_SERVER[‘REQUEST_URI’];

if( strpos($link, ‘product/’)!==false ){
$new_link = get_bloginfo(‘siteurl’).str_replace(‘product/’, ”, $link);
Header( “HTTP/1.1 301 Moved Permanently” );
Header( “Location: $new_link” );
exit();
}
}
add_action(‘init’, ‘base_category_redirect2’, -9);

Since I am not a programmer and I do some manipulations with the inserted method – it works, I cannot explain the principle of operation. But this method helped me solve the redirect problem.