Monday, January 19th, 2009 | 21 Days of WordPress Tips, Code, Tips, WordPress

WordPress Tip: How to change the domain name

This post originally appeared by Tim Grahl in our blog at Out:think Group, if Tim is referenced in the comments, that's why.

While it’s nice to build a new site behind a coming soon page, in most cases when you are designing a new site you’re doing it while a current site already exists. There are several different ways to do this, but in most cases you’ll test it at a demo domain name and then, when you launch, you’ll need to switch everything over to the live domain name. Unfortunately, this is not as simple as it sounds. Below are a few of the common pitfalls and how you can get around them safely.

Caution: Some of my tips below use SQL statements that change a lot of database content at once. While I use them on a regular basis with no problems, I don’t guarantee that you won’t screw something up. Please backup your database and wp-content folder before doing anything. You have been warned.

1. Hard coded links in your theme

This will not be a problem 99% of the time, however if you’re theme was built custom or you have made changes to it, there might be places where you hard coded a link (i.e. http://mysite.com) instead of using one of the WordPress functions to call it (i.e. get_option()).

If this is a problem, just go through and replace each of the hard coded links with:

<?php echo get_option('siteurl'); ?>

This will pull your URL from the database. Once these changes are implemented, you’ll never need to touch them again even when you change the domain of your site.

2. All the links use the demo domain name

WordPress stores the domain name of your site in the database table called wp_options. If you try to login to your site on the new server it will automatically redirect you to the old domain name that is stored in the database.

What you need to do is open up the phpmyadmin for your server and run this SQL query with the relevant pieces changed to your situation:

update wp_options set option_value = replace(option_value,'http://demo-url.com','http://live-url.com');

This will change all of the references in your wp_options table to the live URL. This will make it so you can now login to the Dashboard and all of your links using the function from #1 will work as well.

Tip: To combat hackers and make the WordPress installation more secure, your options table may not be named exactly “wp_options”. Be sure to check your database first to make sure you have the table name correct.

3. Broken permalinks

When you set your permalinks under the Settings -> Permalinks page of the WordPress dashboard, it changes settings in your .htaccess file in the root of your site. So when you are running your site under a different domain name, these will often no longer work.

Thankfully this is a very easy fix. Merely click the “Save Changes” button on the Permalinks page to reset and it’s done.

4. Post/page images and files no longer load

Assuming you added content to the site prior to launch, you probably uploaded some images (or other files) with the media uploader and added them to the post content. Unfortunately these are now hard coded to the demo domain name.

Thankfully, there is any easy fix that you’ve already had practice with in step #2 above.

Log back into your phpmyadmin and run this SQL statement:

update wp_posts set post_content = replace(post_content,'http://demo-url.com','http://live-url.com');

This will change any references to the demo domain name in your content, whether an image, link, PDF, etc., to the live domain name.

And you’re done

Your site should now run at the live domain name with no problems. However, since hosts and test environments vary greatly, you may run into a problem I don’t mention above or one of my tips may not work for you (i.e. your host doesn’t have phpmyadmin installed). If this is the case, leave a comment below and I’ll do my best to help you out.

Discussion

  1. January 19, 2009

    WordPress Tip: How to change the domain name | Out:think | | Latest domaining news from around the world says:

    [...] Here is the original:  WordPress Tip: How to change the domain name | Out:think [...]

  2. February 7, 2009

    Luis says:

    you will need to run this too to get actual thing working properly even at the level of the media library:

    update wp_posts set guid = replace(guid,’www.olddomain.com’,'www.newdomain.com’)

    Encouragements in Christ Jesus,

  3. February 14, 2009

    Sree Raj says:

    Thanks. I changed my blog name from howyoudoin.info to dudeaboard.co.cc and was confused to prevent it from getting directed to howyoudoin.info . This helped me.

  4. March 14, 2009

    Brooke says:

    THANK YOU! I can’t believe it worked. I heart wordpress and I heart finding wordpress help on the internet. Thanks again!

  5. April 7, 2009

    wanmus says:

    amazing stuff thanx :)

  6. April 27, 2009

    AddetcedeMop says:

    Спасибо за статью. Восхищена как всегда

  7. May 3, 2009

    Blalaymnnamy says:

    На Ваш блог знакомый в аську ссылку кинул. Оказалось ,что не зря Понравилось. Тепрь постоянно читать буду

  8. May 8, 2009

    znydz says:

    I’ve change my domain by follwing your instruction, it works well.

    SO far nothing is wrong.

    My question is, how can I rebuild the sitemap of my new domain?

Leave a Comment