From 1e3f41e06f6495954c1e9f3443aff7e23cde9d44 Mon Sep 17 00:00:00 2001 From: Axiom Date: Mon, 23 Nov 2026 20:45:00 -0800 Subject: [PATCH] Redirect duplicate price and accessories pages to their canonical targets Search Console shows three URLs carrying the identical BRABUS price title and two generic pages competing with stronger ones. A 301 map consolidates each pair without touching post content, and a smoke test pins the behaviour. --- tests/php/redirects-smoke.php | 73 +++++++++++++++++++ wp-content/themes/3w-2025/functions.php | 5 ++ wp-content/themes/3w-2025/inc/redirects.php | 81 +++++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 tests/php/redirects-smoke.php create mode 100644 wp-content/themes/3w-2025/inc/redirects.php diff --git a/tests/php/redirects-smoke.php b/tests/php/redirects-smoke.php new file mode 100644 index 0000000..2808a36 --- /dev/null +++ b/tests/php/redirects-smoke.php @@ -0,0 +1,73 @@ + target path on this site. + * + * @return array + */ +function threew_consolidation_redirects() { + return array( + '/brabus-g-wagon-price/' => '/price-of-g-wagon-brabus/', + '/brabus-price/' => '/price-of-g-wagon-brabus/', + '/mercedes-benz-g63-brabus-price/' => '/price-of-g-wagon-brabus/', + '/car-accessories-online/' => '/best-online-parts-store/', + '/mercedes-mansory/' => '/mansory-brabus/', + ); +} + +/** + * Match the current request against the map and redirect permanently. + */ +add_action( + 'template_redirect', + function () { + if ( is_admin() || wp_doing_ajax() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { + return; + } + + $method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : 'GET'; + if ( 'GET' !== $method && 'HEAD' !== $method ) { + return; + } + + $request_path = isset( $_SERVER['REQUEST_URI'] ) ? wp_parse_url( wp_unslash( $_SERVER['REQUEST_URI'] ), PHP_URL_PATH ) : ''; + $request_path = '/' . ltrim( (string) $request_path, '/' ); + $request_path = trailingslashit( $request_path ); + + $map = threew_consolidation_redirects(); + if ( ! isset( $map[ $request_path ] ) ) { + return; + } + + $target = home_url( $map[ $request_path ] ); + $query = isset( $_SERVER['QUERY_STRING'] ) ? sanitize_text_field( wp_unslash( $_SERVER['QUERY_STRING'] ) ) : ''; + if ( '' !== $query ) { + $target = $target . '?' . $query; + } + + wp_safe_redirect( $target, 301 ); + exit; + }, + 1 +); + +/** + * Purge full-page cache once when the redirect map changes. + */ +add_action( 'init', function () { + $cache_version = '2026-11-23-1'; + if ( get_option( 'threew_consolidation_cache_version' ) === $cache_version ) { + return; + } + + if ( class_exists( 'LiteSpeed_Cache_API' ) ) { + LiteSpeed_Cache_API::purge_all(); + } + + update_option( 'threew_consolidation_cache_version', $cache_version ); +} );