A couple of days ago I was managing to move a blog from one domain to another. It’s not the first time I do that, but still hadn’t found the best way to do it.
After some research, I think I found the best, fail proof way to accomplish it.
To be honest, there are two best ways to do it:
Moving To Domain Root (Home)
If you are moving your blog from olddomain.com to newdomain.com, the best thing you can do is parking the new domain over the old one. If your host supports cpanel, it’s a no-brainer to make the changes.
- Log in to your blog cpanel account and go to Domains > Parked Domains, type your new domain url and hit Add Domain.
- Now you need to change your WordPress installation data, to make everything point to the new domain. Begin with Settings > General and go through every tab to make sure nothing was left behind. Some plugin may require your blog url to work, so it’ll make no harm a rigorous check. Update your permalinks and you are good to go.
- Parked domains use a 302 redirect by default. This kind of redirect means temporary and search engines don’t look at it with a good face. Only use it if you intend to unmake the changes. If you will keep them, then you need a 301 redirect.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule ^/?(.*)$ "http\:\/\/newdomain\.com\/$1" [R=301,L]
Copy the code above, paste it on a text editor (notepad will do fine, if your computer is a PC), change olddomain and newdomain to suit your needs, save it as .htaccess and replace your original one with it.
What it does is pretty simple: it tells browsers that any request to the old domain, with or without www, must call the new one. It includes all internal links, so you won’t miss a single visitor from search engines or links to your posts.
Moving to a Folder or Subdomain
If you want to move your blog to a folder or subdomain of your new domain, let’s say newdomain.com/blog or blog.newdomain.com, the tutorial above is not for you. Parked domains are relative to your home directory, so forget about it.
In this case, you will have a bit more of hard work, but don’t worry, I’ll walk you through the entire process.
- Upgrade your WordPress installation to the latest version at your old domain, in case it’s not already updated. Since we’re going to move everything to another domain, it’s a good idea to have the same version in both places.
- Download your entire wp-content folder from the server.
- Back up your blog database. If you don’t know how to do it, check this topic at WordPress.org.
- Install WordPress’ latest version in your new domain’s folder or subdomain.
- Upload wp-content folder that you’ve downloaded in step 2, replacing the existing one.
- Go to your blog’s database at the new domain and delete everything.
- Import the database you’ve backed up in step 3.
The Fine Tuning
Now we’re half way through, but there’s still a couple of things to solve.
Since you’ve imported the database from your old domain, all links point to that url. You’re going to have to change everything. But fear not, you don’t have to do it mannualy, there are a couple of SQL commands to help you out.
Copy the code bellow, go to your mysql database, click on SQL tab, paste it and hit execute. It will update all your WordPress /Options urls.
UPDATE wp_options SET option_value = replace(option_value, 'http://old-domain.com', 'http://new-domain.com') WHERE option_name = 'home' OR option_name = 'siteurl';
Now you need to update your posts and pages urls.
UPDATE wp_posts SET guid = replace(guid, 'http://old-domain.com','http://new-domain.com');
As a final step, you’ll want to update absolute urls to your old domain that you probably placed within your posts.
UPDATE wp_posts SET post_content = replace(post_content, 'http://old-domain.com', 'http://new-domain.com');
That’s it, everything done.
Now, log in to your WordPress Administration with your usual login and password (you imported your old database, so everything, including your login data and active plugins remain untouched), go to Settings > Permalinks and hit save changes; this will update your permalinks structure.
Check if everything is fine, internal links, single pages, images etc. Take some time to see if anything needs to be fixed.
Everything OK? It’ time to redirect your old domain to the new one, since you don’t wanna lose visitors from search engines or links to your old domain.
If you use cpanel, go to redirect, fill the fields with your data and save. Don’t forget to check wildcard redirect and to choose 301 redirect. Wildcard will redirect all links to your internal pages to the equivalent ones on the new domain, 301 means permanent.
Check a couple of urls from your old domain to see if they’re redirecting fine. If not, download your .htaccess file from your old domain and delete everything related to WordPress, leaving only the redirect.
If you don’t have cpnel, copy the code bellow, paste it on a text editor and save as .htaccess. Upload it to your old domain server and that’s it.
Redirecting to a folder.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old-domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old-domain.com$
RewriteRule ^/?(.*)$ "http\:\/\/www\.new-domain\.com\/your-folder\/$1" [R=301,L]
Redirecting to a subdomain.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old-domain.com$
RewriteRule ^/?(.*)$ "http\:\/\/sub.newdomain\.com\/$1" [R=301,L]
That’s it, hope it will help you. If you have any doubts, please leave a comment.