Having just done this to move from apartmentonesix.com to yourcodegarage.com, I figured it would be fitting to write a quick post about how to handle this sort of redirection.
It’s important to know the goal – in this case, we want to move a site from one url to another. For example, from
http://site1.com
to
http://site2.com
However, we’re actually moving all the content and everything over – so we want to redirect the individual urls, like this:
http://site1.com/post-1/
redirects to
http://site2.com/post-1/
That way any bookmarks or incoming links pointing at the old site will redirect properly at the new one.
First things first, you need to duplicate your WordPress installation from the old site onto the new one. Here’s the drive by version of how to do that:
We’ll be back to thsi site in a bit to implement the redirect, but for now, we’ll move to the new site.
$table_prefix =
You need to make one more change, at about id 39 – look for an option with the option name of “home”, and change it appropriately.All we’ve got left to do is redirect your old site to the new one. Unfortunately, this is often the scariest and most difficult part of the entire process. On the bright side, you should be able to just paste this code in and call it done.
To accomplish our redirect, we’re goign to modify the .htaccess file of our old site. .htaccess is a file that you can use to give your server special instructions on how to handle certain requests to your site. Because it starts with a dot, it’s often hidden by FTP clients – so you may have to do some tinkering to have it visible and editable to you.
Here’s the code we’re going to use:
RewriteEngine on RewriteRule ^(.*)$ http://yourcodegarage.com/blog/$1 [R=301,L]
Obviously, you’re going to want to replace “yourcodegarage.com/blog” with your own url. Just put that at the top of your .htaccess file, and you should be all set. Here’s the gist of what’s happening:
RewriteEngine on
Here, we’re just telling the .htaccess file to make sure the rewrite engine that your webserver uses is running, and able to handle the request.
RewriteRule
We start the line with the word “RewriteRule”, because that’s what we’re defining – a rule for the server to use in rewriting the url and sending the user to where we want them.
^(.*)$
I’m not going to get into the nitty gritty of the regular expression being used here, but what you need to know is: This section says “Match the entire url (after the domain name), and store the whole thing for me to use later in this line.”
http://yourcodegarage.com/blog/$1
At this point, we give the url we’d like to send the user to. In my case, I needed to send the user to yourcodegarage.com/blog, because that’s where the blog is now located. The $1 after that is telling the server to append whatever we found before to the end of this. So, if the old url was
http://apartmentonesix.com/my-post/comments
then “my-post/comments” would be grabbed, and appended to the end of “http://yourcodegarage.com/blog”, ensuring that the user gets sent where he needs to go.
We finish up with this:
[R=301,L]
All we’re saying here is use a 301 redirect (permanent), and don’t listen to any redirect rules after this one.
And with that, we’re all set! Make sure you use Google Webmaster Tools to notify google of the change, and get to work blogging at your new url!
Thanks again Peter. I just talked to my programmer about this and it seems he’s devised a method of pulling all the old URL’s (about 9,700 of them) into a database and grouping them by similarity so we can narrow down the amount of htaccess editing significantly.
I’m actually using that same plugin you recommended on a few other sites and agree it’s a good one.
Thanks for this helpful post and the other pointers.
Thanks Peter.
Can you tell me if the regular expression below will work for a site that has this permalink structure:
http://www.oldsite.com/2010/08/how-to-develop-a-sense-of-purpose/
To a new site with just %postname%
RewriteEngine ON
RewriteRule ^/([0-9]+)/([0-9]+)/(.*)$ http://newsite.com/$1 [R=301,L]
Thanks for that I just successfully redirected my site.
For those that are lazy pay someone $20 bucks to make a copy of your website on your new domain.
Then you can go into the .htaccess files and edit it yourself or you can pay them too.
Worked like a charm. Thanks for the step-by-step guide.
Excellent work and thanks a lot for saving me all the effort or redirecting one by one,
you are a star mate. thanks again for sharing.
Thanks for posting this, worked like a charm.
Hi guys,
I hope that someone can help…
Basically I have decided to change my portfolio url (I am experienced with 301′s but cant think what ive done wrong).
My old site is: http://www.swex.co.uk which did have a blog on it (/blog/). Google has indexed all of the old posts, categories , tags etc on it. I have a .htaccess with my 301′s in it (ModRewrite is on) to divert the old pages to the root of my new site: http://www.mathewporter.co.uk apart from a few relevant URL’s that redirect (or I want to, but they dont work) to new URL’s for the post’s.
Anyway if you didnt follow that, dont worry, here is my issue. 301′s from the old URL go to the new site, but the old URL is appended to the new domain as described in your article… I dont want this, I want the majority to redirect to the home page and a select few old URL’s to redirect to the relevant post’s that have moved, but have new URL’s.
Hope that make’s sense / isn’t to confusing and that someone can help.
Many thanks.
Does it impact if instead of restoring 1) wp-content and 2) phpmyadmin in this sequence, restore first phpmyadmin database and then wp-content.
I am getting error . Can you please guide
The requested URL /income-tax-rates-for-financial-year-2013-2014/ was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Thanks as per above steps , i am able to transfer my domain. Thanks for complete information.
Hi CodeGarage,
I’m having trouble redirecting a website of mine (the one in my name/website). It’s not using WordPress, but I figured the 301 redirect rule would still apply. It’s not working though. Do you know if what you wrote above applies to non-Wordpress websites, or is it WordPress-only?
Thanks for your time.
Regards,
I have a wordpress blog that i using costum post types.
How to redirect my domain1.com/COSTUM/post-name to domain2.com/post-name using htaccess , i mean 301 redirection.
Thanks in advance.
July 22, 2011
Hi Peter,
This is a very helpful post and thanks for that… I just got referred to codegarage by a buddy last night and plan on switching over from my current backup method to Locker ASAP.
Anyways, I’m doing a web re-design for a client and getting their site switched over to wordpress. This post was very helpful but doesn’t really apply since the old internal linking structure from their previous CMS is kind ugly (similar to the WP default with lots of numbers and variables in the URL).
I’ve seen a few solutions for this using plugins but setting up rules for a large site with thousands of url’s seems a bit silly if there’s a simpler solution.
Do you happen to have any ideas or suggestions for doing 301 redirects for re-designed site with loads of pages where the internal linking structure and url’s for the new pages will be entirely different than the old url’s so as not to lose any currently indexed pages in the search engines that are sucking in a little bit of organic traffic?
Thanks!
Chris