<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Wordpress - redirect in functions.php]]></title><description><![CDATA[<pre><code>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 &amp;&amp; $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;
    }
}
</code></pre>
]]></description><link>https://bb.aizu.my/topic/45/wordpress-redirect-in-functions-php</link><generator>RSS for Node</generator><lastBuildDate>Wed, 07 Oct 2026 09:28:40 GMT</lastBuildDate><atom:link href="https://bb.aizu.my/topic/45.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 25 Apr 2024 08:27:38 GMT</pubDate><ttl>60</ttl></channel></rss>