How to 301 Redirect all Subdomains

March 15, 2011   ·   By   ·   1 Comment   ·   Posted in Miscellaneous

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


they’ll be redirected to


which is definitely what you want.

How to 301 redirect your WordPress blog to a new URL

March 5, 2011   ·   By   ·   20 Comments   ·   Posted in Wordpress

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.

What are we trying to do?

It’s important to know the goal – in this case, we want to move a site from one url to another. For example, from




However, we’re actually moving all the content and everything over – so we want to redirect the individual urls, like this:


redirects to


That way any bookmarks or incoming links pointing at the old site will redirect properly at the new one.

The Steps

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:

On the old site:

  1. Grab a backup of your files
    You don’t have to actually grab the whole wordpress install (with it’s roughly 1000 files) to accomplish this – chances are, you’ll be just fine with just the wp-content folder. Download these files to your desktop.
  2. Make a dump of your database
    Using phpMyAdmin (or some other backup utility), get a copy of your database. Download this to your desktop also.

We’ll be back to thsi site in a bit to implement the redirect, but for now, we’ll move to the new site.

On the new site:

  1. Upload your file backups from the old site.
    If you only downloaded the contents of the wp-content folder, you’ll need to install a fresh copy of WordPress over here, and then upload your wp-contents folder, overwriting the one that was installed with WordPress.
  2. Import your database backup to the new site
    Again, you’ll need to use phpMyAdmin, or a similar client to handle this. Depending on the new hosting setup, you’re probably going to need to create a database and a database user for WordPress to use – but that should be covered by the WordPress install process from step 1. Take Note! of your database prefix. If you’re not sure what this is, you can look at the wp-config.php file on your old site (look for a line that starts with
    $table_prefix =
  3. Connect to the database
    If necessary, set up or modify the wp-config.php file for your new site, making sure it points to the database tables you just imported. You’ll know you’re on the right track when you load up the new url, and you see your blog content there, waiting for you. The styling might be messed up, and clicking any of the links will take you to the old site – but if you can see your content, you’ve done everything right so far.
  4. Switch WordPress’ location values
    If you’ve tried to log in at the new site before this point, you probably had something confusing happen – you went to wp-login.php, entered your credentials, and suddenly you were back at your old URL! This happens because of some location settings in the database files you just imported. To fix it, you need to change 2 values in your database. Again, you’ll need phpMyAdmin, or similar, to make these changes. The first option you need to edit is id 1, with the “option_name” of “siteurl”. This should currently be the url to your old site – you want it to be your new site. Change it to that, with no trailing slash:
    Site Url Option in phpMyAdminYou need to make one more change, at about id 39 – look for an option with the option name of “home”, and change it appropriately.
  5. You’re all set! The blog should now be working at it’s new location, completely.

Redirect code for your old site

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.

What are we doing?

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.


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.”


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


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:


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!