aizu's Knowledge Base
    • Go to: aizu.my Homepage
    • Recent
    • Tags
    • Register
    • Login

    Wordpress - redirect in functions.php

    Ref
    1
    1
    55
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      aizu
      last edited by aizu

      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;
          }
      }
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post