Just a quick post – I recently had a friend who was moving from a WP MultiUser setup, where each blog had its own subdomain, to one consolidated blog. He wanted to maintain links pointed to his old blogs, so he needed to 301 redirect all the pages on the old subdomains to the appropriate pages on the main domain using his .htaccess file. The code to put it together wasn’t too tricky:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^yourdomain.com$ [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]
This will take any request your site receives, and make sure that the url starts (after the http://) with yourdomain.com. If it starts with anything else (say blog.yourdomain.com, or even www.yourdomain.com), it will be redirected to http://yourdomain.com. This code also allows for deep redirection – that is, if the user typed in
blog.yourdomain.com/my-favorite-page/
they’ll be redirected to
yourdomain.com/my-favorite-page/
which is definitely what you want.
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!
Recent Comments