How to move WordPress blog and redirect traffic to a new domain

My old WordPress blog is located at http://www.codingbeaver.com and I would like to move it to the new location at https://www.learningpenguin.net, also I would like to redirect user automatically to the same post on the new site. For example, if user comes to my old site through search engine for a post like this: http://www.myolddomain.com/index.php/2009/11/30/wordpress-won-2009-open-source-cms-award/, I would like to redirect them to my new site as this: https://www.learningpenguin.net/index.php/2009/11/30/wordpress-won-2009-open-source-cms-award/, here is how I did it:

1. Use phpMyAdmin to export the WordPress database from the old site to a sql file and save it to my local computer.

2. Download a copy of the main files from the old site to my local computer.

3. Create a MySql database on the new site.

4. Use phpMyAdmin to import the sql file to the new database.

5. Use phpMyAdmin to open the new database. Because WordPress stores the absolute path of post in database, I need to manually update the old path to the new path. Thanks to this article, I ran the following three queries to update all paths:

UPDATE wp_options 
SET option_value = replace(option_value, 'www.myolddomain.com', 'www.learningpenguin.net')
WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts 
SET guid = replace(guid, 'www.myolddomain.com','www.learningpenguin.net');
UPDATE wp_posts 
SET post_content = replace(post_content, 'www.myolddomain.com', 'www.learningpenguin.net');

Another way which is also easier way to do is to open up the exported sql file and replace all old host name entries with the new host name, then save the sql file.

6. Modify wp-config.php with the new database information.

7. Use FileZilla to upload all WordPress main files on my local computer to the new site.

8. The new site is up and running, but I need to modify the old site a little bit so it can redirect traffic to the same post on the new site. Find header.php file under the template that I use on the old site, and add this JavaScript function to the <head> section:

<script type="text/javascript">
function redirect()
{
    var oldUrl = location.href;
    if (oldUrl.indexOf("myolddomain") != -1)
    {
        var newUrl = oldUrl.replace("myolddomain.com", "learningpenguin.net");
        location.href = newUrl;
    }        
}
 </script>

Then modify the <body> tag as this: <body onload=”redirect()”>

Whenever user is visiting the old site, the JavaScript will redirect the user to the new site.

Hope this will help someone.

Leave a Reply

avatar
  Subscribe  
Notify of