Moving WordPress Blog To a New Domain Without Losing Traffic
Last week, MediaTemple servers got exploited. It was pointed out that target where sites with one or more WordPress installations. Whether you were hosted on a (mt) (gs) or (dv) server, no exception were made whatsoever. Certainly, I had the ‘joyful opportunity’ and was lucky enough to get the injection, which was hidden behind a <script> tag, at the end of every single post and/or page in the database. Injecting the malware script in the posts was very impressive (I pat the hacker in the back), as nothing happened whenever an infected site’s home page loaded, so it was impossible to sniff anything strange around. Nevertheless, once a reader clicked a post/page title, he/she would have been redirected to the infection injecting site, in about the next one or two seconds. It wasn’t quite pleasing tho, but sometimes this things give you a fun experience, like for instance spending half of the day trying to put order back again in the sweet Grid-Server (I headbutt the hacker, badly).
Fortunately, it all came well to the end, but I was so hasty that I clean installed all my WordPresses. After realizing that all did was completely unnecessary, I decided to move iTechnologize.net to this other domain of mine for good. I am not writing this article to express my condolences to neither MediaTemple or show how touched I felt. Instead, I want to tell you the procedures I went through, to moving WP from one domain to another. Keep in mind that this is not going to be an exhaustive list, so I’ll just walk you through the process quickly and easily I hope, and in the end I’ll show you what I did to redirect the domain and all my posts to cope with the new permalink structure I am using now. I am fully aware that you might have read a zillion articles regarding this, however this is the way I did it, and would do it over and over again. It’s a little risky hence it’s not recommended you try it at home, just on clients, because I want them to work with me now.
P.S. the steps are very concise but not detailed, so I expect you have a little or some experience with maintaining a server, using phpMyAdmin and FTP. But you can ask questions if more explanation is necessary. I’ll be glad to mess your head even more.
1. Backup
So you’ve got your new domain (location) up and running. Now first things first, I’ve seen many people tell to back-up all the files. I honestly don’t find it necessarily important to do that especially when you do a move. There are, however, some files that need to be temporarily saved for the next installation. Here’s a short and simple list of my considerations for back-up.
- wp-config.php is the important one, but only when I plan to use the existing database.
- everything that lies within wp-content folder. Thus, plugins, themes and uploads folder.
But of course it all depends on your folder structure (i.e. where is uploads folder, any other files you use outside your theme’s folder, etc.). - What you must exclusively backup is the database of course, but indeed that is a no-brainer.
That would basically suffice for the next move.
But when I speak of backing up the database, here you might want to consider two alternatives. Both rely on different scenarios.
- Use phpMyAdmin to export your current database, if you plan to use the same in your new location,
- Use Tools > Export if you want to preserve the existing content only; posts, pages, tags, categories etc. This means, however, you will lose all other settings, so you will have to reconfigure everything.
One more thing here and we’re done! In order to save you some minutes of frustration later, a good thing to do at this point would be changing some site configuration values. Log in to your old site’s WordPress Admin, click on Settings > General and change your WordPress address (URL) and Blog address (URL) to point to your new domain name. Doing this will spare you a WTF?! surprise, at the point when everything is ready on your new site, you try to login to you WP admin panel, and all of a sudden you get redirected back to the old site. If by any chance you forget to accomplish this move at this stage, don’t shit your pans because the same can be done later through phpMyAdmin with a simple SQL statement.
//update WordPress options with the new blog location: UPDATE wp_options SET option_value = replace(option_value, 'http://www.itechnologize.net', 'http://wp.xhezairi.com') WHERE option_name = 'home' OR option_name = 'siteurl';
And that’s it, export your database now, selecting either of two alternatives above.
2. Prepare Your New Site
Now if you didn’t setup your new site before, now is a good time to make it ready.
The usual steps would consist of:
- New-Domain.com NS change so it directs to your host,
- Create the database
- WordPress Installation – download, extract and upload files, or use the famous 1-Click installer if your hosts provides one,
- Upload/Overwrite all your backed up files from the old-domain.com
- Tools > Import your content, or phpMyAdmin > Import SQL file,
- Setup any vital changes if necessary.
Now, before moving further, I have to tell you that up until now you’re almost ready to start freshened with the new location. Make sure your new site is configured an running smoothly, simply by loading the new-domain page on your browser. If everything seems right, you’ve got your posts and pages there, what you must do now is update old site links with the new ones. This can be achieved very easily, again simply by using phpMyAdmin.
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.itechnologize.net', 'http://wp.xhezairi.com');
Navigate around on your new site a little, just to confirm whether links are okay and site is loading the content properly. If you consider it to be fine, then you’re ready to go on the final step, and that is the most attractive one.
To me at least.
3. Finalizing – 301 Permanent Redirect
And you’re ready to move along, except for one last thing. We don’t want the old domain to generate any more traffic. That is the intention of the move of course, else why would we want to deal with all this boring steps, it would be all in vain. So at this stage, we do a 301 Permanent Redirect on the old site, and this is the case where we preserve all the traffic value and tell the search engine that we’ve moved for good.
The 301 Redirect can be achieved by putting this lines in a index.php file
<?php Header( "HTTP/1.1 301 Moved Permanently" ); Header( "Location: http://www.new-site.com" ); ?>
but I prefer using .htaccess over PHP in this situations, for one good reason only. RegEx
Open Notepad or any other plain text editor of choice, and copy / paste the lines below in it.
Options +FollowSymLinks RewriteEngine on RewriteRule (.*) http://wp.xhezairi.net/$1 [R=301,L]
If you’re on Windows, save the file with a name of your choice, because you won’t be able to name it .htacccess, Windows doesn’t allow naming a file only by it’s extension. Upload it to your public folder and rename it to the required name (.htaccess), but you know that already. Don’t forget to replace my domain with yours, or you can even leave it just like it is, and I may thank you for that one day.
I first put this file in the root folder of itechnologize.net (which is my old domain), and in my case is html (or public_html depending on your host). This would redirect all the old domain traffic and all sub-directories to same directory structure on wp.xhezairi.com (my new domain). And I can see you go crazy about it now, because it seems so easy with just three lines of code, and you’re so happy about it.
But what if during the move, you’ve decided (like I did) to change the permalink structure of your posts, and make a better change in your new blog. “I want to make my permalinks more SEO this time” you say, and you don’t want to lose the traffic, an old post link should redirect to the original post regardless of its new permalink structure.
I hear you.
In my situation, my permalink structure used to be like the following:
http://www.itechnologize.net/20090301/mediatemple-cluster-2-incident-itechnologize-offline-for-6-hours/
I changed it to http://wp.xhezairi.com/mediatemple-cluster-2-incident-itechnologize-offline-for-6-hours.html
You see, I got rid of the date part and the trailing slash at the end of the URL, plus I added the .html suffix to it. So I wanted to redirect new visitors here, those who find my old saved posts by searching in Google.
This is the .htaccess file I had to write in order to redirect all the permalink transformation and preserve the traffic for my new site.
Options +FollowSymLinks RewriteEngine on RewriteRule ^([0-9]+)/(.*)/$ http://wp.xhezairi.com/$2.html [R=301,L]
The Regular Expression above is simple and easy. What it basically tells the browser is this:
- ^ – Start of line anchor (the .htaccess file lies within the old domain so it knows its location,
- ([0-9]+) – Combination of numbers, 1 or N (my published post date remember?)
- (.*) – Any single character combination, * indicates 0 or N
- $ – End of line anchor
- Last, but not least, the domain where to redirect
Mark the $2 characters in the link. It retrieves the value from the second grouping of characters only.
That’s the part where I got rid of the date numbers.
Finally I patted myself on the back, sat back and drowned in the smoke of a cigar, while staring at the new blog home immensely.
That is it more or less. I encourage you to ask any question you regarding this by using the comment form below, and I am gladly willing to help you, but first let me tell you one more thing.
This article consist of more than 1700 words, that is more than writing a freaky essay. So if you don’t go ahead to use the Social Buttons and Digg it, Tweet about it or take it to the Buzz, it will break my heart thoroughly, and make me feel like the examiner didn’t even bother scan through my essay. Then I will be forced to shave your head, and blame Matt for making such a big deal out of the Thesis theme. Or I would blame Chris for having such a haughty attitude and being such a great developer. Or eventually I will thank you for the time you took reading it, and for spreading the word and letting the others know about it.


Can you transfer database without phpMyAdmin , without losing trafic , I mean is there another way to do it ?
If by ‘transfer database’ you meant export database, then yes there are other ways of exporting it.
One might be using a WordPress Plugin (i.e. WP-DBManager).
You made my day man! I’m off to transfer my blog now. :)