Archive

Posts Tagged ‘Moving Wordpress’

Moving WordPress blog how-to, new domain new host

February 13th, 2010

My hope is that I don’t have to go through this again, ever. But if I do, it will be much less painful from the lessons I learned. Moving a WordPress blog is not just simply a quick copy and paste of the entire directory… especially if you are also moving it to a new domain name. There are export options within WordPress and other tools, which can be a bit hit and miss so I decided to do it this way. To save others from struggling on their own, I decided to write up a guide and to shed some light on some of the pitfalls.

Back up your files and database!
Before commencing any work, make a back up of the following as a minimum requirement:

1. Load up your favourite FTP client and make a copy of these files and folders from your existing site.

  • /wp-content/plugins
  • /wp-content/themes
  • /wp-content/uploads
  • .htaccess file (this is a hidden file by default, view all hidden files within your client to see it listed)

If you can make an entire copy of all folders within your site, then please feel free.

2. Make a copy of the WordPress SQL database by exporting it into a .sql file. Most hosters have SQL tools available, such as phpMyAdmin within cPanel to do an export.

cPanel - phpMyAdmin tool

phpMyAdmin - Export tool along top toolbar

When doing the database export, make sure all tables are selected. It may also be an idea to backup each table into individual files, I’ll tell you why in a moment…

Install WordPress
Now do a new install of WordPress on your new webspace and create a new SQL database. During the install process, make sure the table prefix matches exactly as your existing database. This is defined in the wp-config.php file:

WordPress table prefix

Just to add, I use EditPlus as my PHP editor which works really well.

Once WordPress is installed, delete all the tables in the newly created database using phpMyAdmin etc.

Import and upload
Now to put all your content back in. Import the .sql database file you backed up earlier and import it into the currently tableless database. You should now see all the populated tables, which contains all your blog settings and posts.

Upload the plugins, themes and uploads folders from your backup via FTP to your new WordPress install. Bear in mind to upload them to the same folder structure, replacing any existing folders. If you are using the same domain name as before and just changed website host, then you are pretty much done. You may need to put your old .htaccess file back in, if you cannot get permalinks to work as intended. If you bought a new domain name then you still got some work to do…

Replacing old domain name references with new. Welcome to SQL!
This is where the fun begins, you will now learn a few SQL commands which will come in very handy in this instance. Making SQL changes directly to the database instead of looking through WordPress settings via the GUI is my preferred method. Also some of these changes can only be made via SQL. Go back to trusty phpMyAdmin again, and load up the SQL tool:

phpMyAdmin - Executing SQL commands

  • Changing the HomeURL and SiteURL – The HomeURL and SiteURL is an absolute path to your site. Since your site location has changed, we need to update this too or else your blog won’t load.
    • UPDATE wp_options SET option_value = replace(option_value, 'http://www.youroldsite.com', 'http://www.yournewsite.com') WHERE option_name = 'home' OR option_name = 'siteurl';
  • Changing the URL for Content – WordPress loves absolute paths instead of relative, so again another change is required to make sure the URL for content in each post record is updated to the new.
    • UPDATE wp_posts SET post_content = REPLACE (post_content, 'http://www.youroldsite.com', 'http://www.yournewsite.com');
  • Changing the GUID – This is a very important change. The GUID houses the absolute location of where your posts are. Visitors to your site won’t be able to get to your articles as it will still be pointing to the old address.
    • UPDATE wp_posts SET guid = REPLACE (guid, 'http://www.youroldsite.com', 'http://www.yournewsite.com');
  • Changing GUID for attachments/images – You may not need to do this, but if you find images and galleries are broken then you may need to update the GUID for the location of images. The best way is to check the current GUID location for images before making changes:

    phpMyAdmin - GUID for attachments

    • UPDATE wp_posts SET guid = REPLACE (guid, 'http://www.youroldsite.com', 'http://www.yournewsite.com') WHERE post_type = 'attachment';

Pitfalls and user awareness

SQL Oops
Not everything went to plan, it was 2am and I think I mistyped one of the SQL commands. Lo and behold the entire GUID column was blank in the wp_posts table. GASP! Remember earlier I mentioned backing up each table into individual .sql files? Well this is where my life was saved. I made several changes to other tables in the database and the last thing I wanted was to restore all the tables and start again. When you do a SQL database backup at the top level, all tables are backed up into a single file. But since I also did seperate table backups, I did a quick restore for wp_posts only and voila! I executed the correct find and replace command again and everything was good to go.

Permalinks and .htaccess
Another issue I came across were permalinks, I like them to look pretty. For some reason, all my permalinks were now coming up with /index.php/2009-01-nameofpost. What was index.php doing there in the URL? It definitely wasn’t there on the old site. I changed the setting within WordPress to try to prevent that from being appended, but it didn’t work. It looked like the .htaccess file was not updating correctly, WordPress normally updates this automatically to set the use of permalinks. If your web host uses Apache, then it will most likely be utilising the mod_rewrite module for this to work. I restored .htaccess from backup which fixed it, but here’s what mine looks like if you need a point of reference:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

And that’s it, job done! It’s actually less painless than it looks, as long as you make backups of your old site then you can always restore back if things don’t go to plan. Good luck!

  • Share/Bookmark

Tech , ,