Changing domains, WordPress

November 2, 2012

After changing my domain (and server) I thought I’d write up a short tutorial on how I did it, so hopefully somebody will have use for it in the future.

The first thing I did was to dump the complete database from my old server and created a compressed archive of my web-root, I then copied these files over to my laptop.
I created an empty database on my virtual local webserver, inserted the database and unpacked the archive on my dev server. My reasoning for this is that I wanted to recreate the new environment locally before I pushed anything up to my new server.

To fully recreate it you’ll also want to have the same domain name locally as the new domain name your moving to. One way is to actually point the DNS records against yourself, which is rather cumbersome, and something I suspect nobody has the patience for. Instead, you can just happily mod your local host file to point your new DNS name against your dev server. After that it’s just a matter of setting up the virtual host for the domain in your apache configs.

# The syntax for the hosts file is IP DOMAIN
127.0.0.1 example.com

So now you should, in theory, have a replica of the real thing running locally. It’s now time to actually change things. Because my page runs on WordPress, I’ll only talk about that, but the procedure is should be pretty much the same no matter what kind of blog engine you have.

If you try to visit the page at this point, you’ll probably end up with a white page and some errors in your error log. The reason for this is because we did a straight database dump from our server, the wordpress settings are not correct for our new domain.
There’s really only two options you need to change to get the site running, WP_HOME and WP_SITEURL, and there’s three ways of accomplishing the change.

One way, which is not recommended to use as a long term solution, but is fine during the development process is to modify these variables with the function define() in your wp-config.php.

define('WP_HOME','http://example.com');
define('WP_SITEURL','http://example.com');

The good thing with this approach is that it doesn’t modify the data in the database, but then again, when your moving domains, that’s generally what you want.

The second approach is to actually update the fields in the database, you can use the function update_option() to accomplish this programmatically. Don’t add these lines in the wp-config.php, you should instead add them to your functions.php in your theme.

update_option('siteurl','http://example.com/blog');
update_option('home','http://example.com/blog');

The last option, my preferred option, is to run a query against the database, and just update the options.

UPDATE wp_options SET option_value = replace(option_value, 'http://oldsite.com', 'http://example.com') WHERE option_name = 'home' OR option_name = 'siteurl';

Right, so now the sites running locally, but all internal links in your posts still point against your old domain as well as the static urls (GUID, usually used as a unique identifier for a post) are wrong, so lets update them.

Update GUID

UPDATE wp_posts SET guid = replace( guid, 'oldsite.com','example.com' );

Update internal links

UPDATE wp_posts SET post_content = replace(post_content, 'oldsite.com', 'example.com');

And that’s it, now we’re done with the data side of things. Verify that things are working as expected, remove the entry in the host file and upload the content (database and web-root) to your server where your new domain points. Set the virtual host for the page on the production server so it serves your content.

It’s important to understand that when you change your domain it doesn’t only become harder for your human readers to find you, but also the non-human reader (Read: web crawlers/spiders). So lets fix that.

You’ll want to tell Google about the domain change, you do this through the Google service Webmaster Tools. And in order to do this, (and this is where I screwed up at first) you need to verify your second level domain name, while I had only verified a sub-domain. In other words, www.beije.fi was verified but not beije.fi. And I had already made my 301 permanent redirect, so I had to jump through a lot of hoops to get things right. In short, verify both of your domains and point the old one to the new one.

So once Google knows you’ve moved, you now need to redirect your old domain to the new one. And you want to do this in order for older links to still function, and so that the users that know your old domain will automatically be redirected to the new one. There’s two ways of doing this, redirection with your DNS registrar or with a simple .htaccess file. I chose the .htaccess file, (which I’m glad for, considering the problem I had to verify beije.fi). But if you can, do both!

I’ll give you a small snippet that you can use for the .htaccess part;

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

Changing domains will probably impact you traffic, especially if a lot of it comes from Google, negatively. To counter some of this you’ll want to change the old domain to the new one as fast as possible on as many places that you’ve got control over it. And another last tip is to spread as many links, but not spam, pointing to your new domain so Google will hopefully pick up the change as fast as possible.

Tags