Moving A WordPress Site To A Different Domain Name

Suppose you want to make a copy of your existing WordPress site on a different domain name (e.g. test.domain.com). Instead, maybe you want to move your site to a different domain name (e.g. newdomain.com). This procedure assumes you have shell access to the Linux server hosting your site running Apache & MySQL.

  1. Copy the web directory:
    cp -pR olddomain.com/ newdomain.com
  2. Dump the MySQL database
    mysqldump -l wordpress-old > wordpress-old-dump
  3. Login to MySQL. Create a new database and load from the dump:
    create database wordpress-new;
    use database wordpress-new;
    source wordpress-old-dump;
  4. Create a new database user for the new WordPress instance:
    CREATE USER 'wordpress-user'@'localhost' IDENTIFIED by 'some-password';
  5. Give your WordPress MySQL user permissions on the new database:
    GRANT ALL ON wordpress-new.* TO 'wordpress-user'@'localhost';
  6. Edit your new wp-config.php: enter the appropriate database name, username, and password:
    define('DB_NAME', 'wordpress-new');
    define('DB_USER', 'wordpress-user');
    define('DB_PASSWORD', 'some-password');
    
  7. Configure the new site in Apache: The steps here are different depending on your distribution. Essentially, you want a new site pointing to the new web directory. Relevant items:
    ServerName newdomain.com
    DocumentRoot /path/to/newdomain.com
  8. Log back into MySQL and fix-up the URLs:
    use database wordpress-new;
    
    UPDATE wp_posts SET post_content = replace(post_content, 'http://olddomain.com', 'http://newdomain.com');
    
    UPDATE wp_posts SET guid = replace(guid, 'http://olddomain.com','http://newdomain.com');
    
    UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://olddomain.com','http://newdomain.com');
    
    UPDATE wp_options SET option_value = replace(option_value, 'http://olddomain.com', 'http://newdomain.com') WHERE option_name = 'home' OR option_name = 'siteurl';
    
    
  9. That’s it! Open up a browser and go to your newdomain.com. You should check your site for any hard link references to olddomain.com in Pages, Posts, and RSS Feeds.