Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions tests/php/redirects-smoke.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Smoke test for the consolidation redirect map.
* Run: php tests/php/redirects-smoke.php
*/
define( 'ABSPATH', __DIR__ );
$GLOBALS['actions'] = [];
function add_action( $name, $callback, ...$args ) { $GLOBALS['actions'][ $name ][] = $callback; }
function is_admin() { return false; }
function wp_doing_ajax() { return false; }
function get_option( $name ) { return false; }
function update_option( $name, $value ) {}
function sanitize_text_field( $value ) { return $value; }
function wp_unslash( $value ) { return $value; }
function wp_parse_url( $url, $component = -1 ) { return parse_url( $url, $component ); }
function trailingslashit( $value ) { return rtrim( $value, '/' ) . '/'; }
function home_url( $path = '' ) { return 'https://www.3wdistributing.com' . $path; }
function wp_safe_redirect( $url, $status = 302 ) {
$GLOBALS['redirect'] = [ $url, $status ];
throw new RuntimeException( 'redirect' );
}
require __DIR__ . '/../../wp-content/themes/3w-2025/inc/redirects.php';

function check( $condition, $message ) {
if ( ! $condition ) {
fwrite( STDERR, "FAIL: $message\n" );
exit( 1 );
}
}

$map = threew_consolidation_redirects();
check( 5 === count( $map ), 'five consolidations' );
check( '/price-of-g-wagon-brabus/' === $map['/brabus-price/'], 'duplicate price page points at the guide' );
check( '/price-of-g-wagon-brabus/' === $map['/brabus-g-wagon-price/'], 'second duplicate price page points at the guide' );
check( '/mansory-brabus/' === $map['/mercedes-mansory/'], 'mansory duplicate points at the comparison page' );

$runner = $GLOBALS['actions']['template_redirect'][0];

// A mapped path redirects 301, preserving the query string.
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['QUERY_STRING'] = 'utm_source=test';
$_SERVER['REQUEST_URI'] = '/brabus-price/?utm_source=test';
$GLOBALS['redirect'] = null;
try {
$runner();
} catch ( RuntimeException $e ) {
}
check( null !== $GLOBALS['redirect'], 'mapped path redirects' );
check( 301 === $GLOBALS['redirect'][1], 'redirect is permanent' );
check( 'https://www.3wdistributing.com/price-of-g-wagon-brabus/?utm_source=test' === $GLOBALS['redirect'][0], 'target and query preserved' );

// An unmapped path is untouched.
foreach ( [ '/about-us/', '/price-of-g-wagon-brabus/' ] as $path ) {
$_SERVER['REQUEST_URI'] = $path;
$GLOBALS['redirect'] = null;
try {
$runner();
} catch ( RuntimeException $e ) {
}
check( null === $GLOBALS['redirect'], "no redirect for $path" );
}

// Non-GET requests are left alone.
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REQUEST_URI'] = '/brabus-price/';
$GLOBALS['redirect'] = null;
try {
$runner();
} catch ( RuntimeException $e ) {
}
check( null === $GLOBALS['redirect'], 'POST is not redirected' );

echo "PASS: consolidation redirects\n";
5 changes: 5 additions & 0 deletions wp-content/themes/3w-2025/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,11 @@ function threew_2025_is_woocommerce_context() {
*/
require_once get_theme_file_path('inc/seo-class.php');

/**
* Include permanent consolidations for duplicate pages.
*/
require_once get_theme_file_path('inc/redirects.php');

/**
* Configure PHPMailer to use Mailpit for local development.
*/
Expand Down
81 changes: 81 additions & 0 deletions wp-content/themes/3w-2025/inc/redirects.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Permanent consolidations for pages that duplicate a stronger one.
*
* Each entry below was chosen from Search Console data: the source page carries
* the same intent as the target and, in the price cases, the identical title.
* Redirecting consolidates the signals instead of splitting them across URLs.
*
* @package 3w-2025
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Source path => target path on this site.
*
* @return array<string,string>
*/
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 );
} );
Loading