browser icon
You are using an insecure version of your web browser. Please update your browser!
Using an outdated browser makes your computer unsafe. For a safer, faster, more enjoyable user experience, please update your browser today or try a newer browser.

WordPress – Cloning your blog into a subdirectory

Posted by on January 18, 2012

Suppose you want to create another wordpress site within your current wordpress domain – but inside a subdirectory. How do you do it?

In our case, we wanted a wordpress site for our kids to post to during our planning and travel, but didn’t want to give them access to our main site. We wanted them to have their own subdomain, but wanted to retain the same theme to start, but would be totally devoted to their own content.

The process, in a nutshell, is this:

0.  Back everything up
1. Create a new wordpress database
2. Copy the current database into the new (clone the database)
3. Copy current wordpress files to subdirectory
4. Make changes to configuration

Let’s get started!

Make backups

The very first step you should take is to backup everything in your current wordpress site. This means the database, the file system, everything. In case anything goes wrong, you want to be able to easily restore things to their former glory.

 

Create a new wordpress database

To do this, you’re going to need shell access to your hosting account. Use your favorite ssh client (Terminal under Mac, Putty under Windows) to log in to your web server.

The following will create a new database on your webserver:

    mysqladmin create <<DB_name>> -u <<DB_user>> --password=secret

for example, you might type

    mysqladmin create subwordpress -u root --password=secret

msysqladmin is the program that manages your wordpress database
create tells mysqladmin to create a new database
-u indicates that we’ll be specifying a mysql user id
DB_Name is the name of the new mysql database
DB_user is probably the root user of mysql
instead of “secret” you should use the real password for root

 

Copy the current database into the new

At the command prompt, type

     mysqldump -u DB_user --password=secret orig_DB_name | mysql -u DB_user --password=secret clone_DB_name

for example, you might type

     mysqldump -u root --password=secret wordpress | mysql -u root --password=secret subwordpress

you should see your prompt return in just a few seconds, indicating that the cloning was successful

 

Copy current wordpress installation to subdirectory

go to your wordpress installation directory

    cd /var/www/htdocs/wordpress

obviously, this will vary depending on your installation.

Once there, issue the command to create a new subdirectory:

    mkdir subwordpress

now copy all of the files from your current wordpress installation into the subdirectory:

    cp -R ./ ./subwordpress

Pay special attention to the spaces.  There are exactly three spaces in this command, one between each of the terms listed below:

cp is the copy command
-R indicates that all directories should be recursively followed (copy all subdirectories)
./ means “this folder”
./subwordpress means “the folder named subwordpress in this folder”

you’ll get some errors at the end, indicating that the current directory cannot be copied into itself. Don’t worry about it.  You wouldn’t want to do that, anyway.

 

Make changes to configuration

It’s a bad idea to use the root mysql user to access your wordpress database. Instead, we’ll use the mysql user in the current wordpress installation to manage the subdatabase as well.

Go to your new subwordpress directory and then edit wp-config.php to see the mysql user and password:

    pico wp-config.php

you will find (among other lines)

define('DB_NAME', 'wordpress');
define('DB_USER','wpuser');
define('DB_PASSWORD','secret');

your name, user, and password will be different.

Now edit that first entry so it reads

    define('DB_NAME', 'subwordpress');

This will tell the new wordpress installation to use the ‘subwordpress’ database. Next you want new subwordpress database the same user and password as the existing wordpress database.  Save and exit.

From the command line, issue the command

    mysql -u root -p

after entering the root mysql password, this will log you into mysql as root. Now at the mysql prompt,  issue the commands to add the wordpress user to the new database

    grant all on subwordpress.* to wpuser@localhost;

To exit mysql, use the command

    exit;

this will add all privileges on the new database to the existing wpuser. Substitute your database username for wpuser.

Finally, we need to change the wordpress configuration details.
First, we need to change the siteurl. The easiest way to do this is to log in to phpmysql, and find the subwordpress database. The first record in the wp_options table is “siteurl”. Change this record from “http://yoursite.com” to “http://yoursite.com/subwordpress” where subwordpress is the subdirectory you’ve chosen.
You’ll also want to change the “home” record to this same value, “http://yoursite.com/subwordpress”.

 

You can now log in to your new subwordpress site

Go to http://yoursite.com/subwordpress/wp-login.php and login with your usual login id and password. You should create a new post to verify that everything is set up correctly – that the new posts only show up in the subdirectory, not on the main site.

Once you’ve verified that everything is hooked up correctly, now go ahead and create new posts, delete old posts (which have been carried over from the main site) and modify the site to your liking.

Congratulations; you’ve just cloned your wordpress installation in a new subdirectory!

11 Responses to WordPress – Cloning your blog into a subdirectory

  1. Fred

    Bret – Love this – I also love using subdomains for ease of naming authors etc.

  2. Amy

    So awesome! I never knew this was possible. I can think of a million reasons why this would be great to do. Thank you so much!

  3. Tracie

    Bret’s pleasure, Amy! You know there’s no way I would have figured it out! HA!

  4. Tracie

    Happy to help!

  5. Pedro

    Hey Bret – this looks great. You’re putting me on the right track. However – i am not sure if this works for what i am planning. I also like to install a second WP theme in a subdirectory.
    This is something i am doing for a friend – setting up a WP theme for his own website…
    and then when i am finished move everything onto his server…

    in the meanwhile i like my website working (also a WP theme) while i am working on my friends theme in a subdirectory of my webspace…

    Do i need a different user/log in to manage a different theme in a subdirectory?

    best regards
    Pedro

  6. Bret

    It looks like you have a completely rational plan. In the parent (root) directory, you have the live blog, your active site. You create a subdirectory using the instructions above, and we’ll call this the “test” blog.

    You can make any changes you want to to themes or code in the test blog, and it won’t affect the parent blog at all. This is a typical production/test environment.

    If you do this, and get your new theme looking the way you want, then you can just package up your theme files and send the finished theme to your friend.

    Good idea!

  7. Eric

    How simple would this be to turn into a cloning plugin for development purposes? I’d imagine a plugin for the main site that would allow you to enter a new subdir name, new credentials, and configure overwrite options, and with one click clone your entire site to a subdir.

  8. Bret

    I concur – I just went through this process again last week, setting up a new sub-site here, and was happy to have taken the time to write out the instructions in the level of detail that I did. There are really just a couple of “inputs” to the process (the main one being the subdir name) and the rest is boilerplate. Unfortunately, there’s some MySQL permission work, some shell commands, and a config file edit in there, so it would be a potentially deadly plugin if it were written without the appropriate security. I know for certain that *I* won’t be turning it in to a plugin, but I’d love it if someone more skilled took on the challenge.

  9. Paul

    Would this work with WP Multi-Site when trying to clone the main site into a sub site?

  10. Bret

    If the main site already has children, then it will work… but you’re not going to see the sub-sub children without some additional work.

    In other words, if you have main and main.origsub you could clone everything into main as subsite, you get three sites: main, main.subsite and main.origsub. main.subsite.sub would exist on disk but would not be navigible, as you haven’t created the database entries for it, nor have you set up the WP config files.

  11. Michelle

    It worked well for me. THANK YOUUU :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>