Redirect traffic from a Wordpress site
Redirect traffic from a wordpress site


The Problem:

A Wordpress site has deprecated and you want to redirect all of its traffic to a landing page on a new site. However, you want to explain why the site has been deprecated in the context of the old site.

The Solution:

You should explain to the user why they are being redirected, so create a page on the old Wordpress site at some url, say: www.oldsite.com/we-moved

On that new page (/we-moved), we want it to have a countdown and then redirect to the new landing page at, say: www.newsite.com/welcome-oldsite

To do the redirect, put the following jQuery/javascript on the ‘we-moved’ page:

<div>Redirecting in <span class="redirect-in">20</span> seconds...</div>

<script type="text/javascript">
jQuery(document).ready(function($) {
  var countdown = setInterval(function(){
    sec=parseInt($(".redirect-in").html()); 
    if (sec != 0) {
      $(".redirect-in").html(sec-1);
    } else { // countdown is finished...
      clearInterval(countdown);
      window.location="http://www.newsite.com/welcome-oldsite"
    }
  },1000);
});
</script>

You can adjust the number of seconds before redirect by changing the value of the .redirect-in span.

Now the redirect to the new site is working.

Now we need to redirect all the traffic to www.oldsite.com urls to the /we-moved page. With the exception of the login page (wp-login.php) and the admin area (wp-admin). In addition, we want to be be able to view the pages on the oldsite from the ‘View Page’ link on the edit pages.

To do this we modify the section of our .htaccess file that looks like:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

And change it to look like:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{HTTP_REFERER} !^(.*)oldsite.com/wp-admin(/.*)?
RewriteRule !^(wp-admin(/.*)?|wp-content(/.*)?|wp-includes(/.*)?|wp-login\\.php(.*)|we-moved(.*))$ we-moved/ [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Thats it. You should be good to go…