Move WordPress database to a new domain name or directory for free

Move WordPress database to a new domain name or directory for free

If looking for a more thorough walk-through of moving files and databases from server to server, visit Migrating WordPress to a New Server.

In case you’re interested, you can also move between WordPress.com and WordPress.org.

Several years ago, I did a WordPress install at domain.com/blog and had a static homepage (i.e. not served up by WordPress). I’m not going to tell you that you should or shouldn’t do that. I’m going to tell you how to go from one to the other. For me, I moved WordPress from /blog to /.

But I was concerned about the database and my URLs. 2 key points:

  1. Below is a simple, 5 minute or less process to change the database references via search and replace.
  2. WordPress handles all the redirects automatically. It’s smart enough to know that domain.com/blog/my-favorite-post is now located at domain.com/my-favorite-post.

It’s Easy

Although it feels like a daunting task — changing from domain1.com to domain2.com, or changing from domain.com/blog to just domain.com, or from domain.net to domain.com — it’s very simple, quick, and easy.

One of the last reference links at the bottom of the Moving WordPress codex page is Search and Replace for WordPress Databases.

From that page, you can download the PHP script that works for MySQL databases (not just WordPress sites) without messing it up (i.e. it works, unlike search and replace in a text editor). Reference the changelog for learning the nitty-gritty details. For most applications, either version of the script — from the website’s download link or from the website’s GitHub link — will work just fine.

4 Simple Steps

All you have to do is follow the instructions. Here is a synopsis of the major steps:

  1. BACKUP FILES AND DATABASE. Then copy files from the old location to the new location (new server, new directory, whatever). If moving to a new server, it may take a while. If moving from one directory to another on the same server, it should take a much shorter time.
  2. Once the files are in the location you want them to be (maybe you didn’t move them at all because you’re just changing from domain1.com to domain2.com on the same server), upload the PHP script from interconnect/it to the same directory where wp-config.php is located. Then navigate to domain.com/name-of-the-script.php (this should take about 2 seconds)
  3. Follow the on-screen instructions (screenshots below) and enter your search-and-replace terms. For example, search for “domain1.com” and replace with “domain2.com”. Tip: Look in your database .sql exported backup file to see how your database is setup — should you search-and-replace with or without a trailing slash? — look for details like that. It’ll ask you to confirm your action, and once you click to proceed, it’ll be done in the blink of an eye. (all this should probably take about 1 minute)
  4. Delete the PHP script from your server. Don’t just rename it; delete it.

Following are screenshots for step #3, above:

Navigate to the script in your browser
Results of auto-importing the wp-config.php settings
Select all database tables
Enter the text to be replaced and the text to replace it with
View the results, maybe save a screenshot for your records

Again, make sure to delete the script after using it. Do not leave it up longer than the 2 minutes it takes to run it. If cannot complete the task immediately after uploading the PHP script, delete it from the server and re-upload it when you’re ready to run it because anyone that can access the file’s URL could run this script; it’s not password-protected.

I hope this quick tip saves you the error of your text editor Find and Replace ways and saves you the time and hassle of using another method. You could even say I just saved you $75+. If you feel inclined, you may donate at the PHP script’s download page.

Please share your experiences in the comment area below.