add_action('template_redirect', 'redirect_old_domain');
function redirect_old_domain() {
$old_domain = 'domain.org';
$new_domain = 'domain.com';
if ($_SERVER['HTTP_HOST'] == $old_domain && $request_uri == '/') {
// Redirect to the new domain without any path
$redirect_url = 'https://' . $new_domain;
wp_redirect($redirect_url, 302); // 302: Temporary redirect
exit;
}
// Check if the request is for a specific year
preg_match('/\/(\d{4})\//', $request_uri, $matches);
$year = isset($matches[1]) ? $matches[1] : '';
if ($year) {
// Redirect to the same URI on the new domain
$redirect_url = 'https://' . $new_domain . '/' . $year . $request_uri;
wp_redirect($redirect_url, 302); // 302: Temporary redirect
exit;
}
}