Wrong category redirections?
-
Hi! i found today strange behaviour.
Our shop page have problem with “category links”.
Example:
We have category “child-category” with path:
https://www.example.pl/category-page/main-category/child-categoryBut for some reason we can also use:
https://www.example.pl/category-page/here-you-can-write-anythingsjjdjdjdjdjdjdjddj/child-category
https://www.example.pl/category-page/xxxxx-ttttt-ddddd/child-category
And there is no redirect to: https://www.example.pl/category-page/main-category/child-category
All this links show page/duplicate? /category-page/no-matter-what-is-here/child-category
Is it normal in wordpress/woocommerce or it’s works wrong? is easy way to fix this?Chatgpt suggest add to wpcode this php… but i’m not sure…
add_action(‘template_redirect’, function () {
if (is_admin() || wp_doing_ajax() || wp_doing_cron()) { return; } if (empty($_SERVER['REQUEST_METHOD']) || $_SERVER['REQUEST_METHOD'] !== 'GET') { return; } // Nie ruszamy adresów z parametrami, np. filtrowania, sortowania, UTM-ów. if (!empty($_GET)) { return; } // Nie ruszamy kluczowych stron WooCommerce. if ( function_exists('is_cart') && is_cart() || function_exists('is_checkout') && is_checkout() || function_exists('is_account_page') && is_account_page() ) { return; } $canonical_url = ''; // Strony, wpisy, produkty i inne pojedyncze typy treści. if (is_singular()) { $object_id = get_queried_object_id(); if ($object_id) { $canonical_url = get_permalink($object_id); } } // Kategorie produktów, kategorie wpisów, tagi i inne taksonomie. if (is_tax() || is_category() || is_tag()) { $term = get_queried_object(); if ( $term && !is_wp_error($term) && !empty($term->term_id) && !empty($term->taxonomy) ) { $canonical_url = get_term_link((int) $term->term_id, $term->taxonomy); } } if (!$canonical_url || is_wp_error($canonical_url)) { return; } $current_path = trailingslashit(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); $canonical_path = trailingslashit(parse_url($canonical_url, PHP_URL_PATH)); if ($current_path !== $canonical_path) { wp_safe_redirect($canonical_url, 301); exit; }}, 1);
But maybe we are missing something in settings? any help?
You must be logged in to reply to this topic.