While checking my email this morning I found a message from Ms. Q wherein she asks me what to do with her blog. She's not looking at stopping but she does want to fix the place up a bit. She wants to update her version of WordPress and possibly change her theme. she also wants to move the blog directory from the subdirectory (/blog) where it currently resides to the root of her domain.
She told me that she went back and read my posts on this topic from when I moved to WordPress from Serendipity and also moved from the /blog subdirectory to my root. After reading those posts she felt a bit intimidated by the idea of constructing the htaccess file full of redirects. I don't blame her! She has about 382 posts on her blog right now and that's a lot of typing!
In an attempt at humor (I hope) she mentioned just deleting everything and starting fresh! Please don't go there.
In this post I'm going to tackle the question of moving the blog from the subdirectory to the root.
Step 1: Backup
That's right. The first thing that should be done is a complete backup of the site. Not just the databases but make a local copy of your entire WordPress installation.
If possible you should be running backups nightly with a minimum of a seven day rotating schedule. You can go longer but I do not recommend going any shorter.
Once all of your backups are complete we move on to...
Step 2: Create the Redirects
The best form of redirect is to create them and post them in an .htaccess file in the root directory of your domain. We use what are known as 301 redirects.
When you are only redirecting a few pages you need to create a new line in your /htaccess file for each page but, in this case we are moving all of the contents of the /blog directory to the root domain.
www.qmusings.com/blog/awesome-article.html -> www.qmusings.com/awesome-article.html
That is the redirect we want and this can easily be done by putting this in your .htaccess file:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^qmusings.com/blog [nc]
rewriterule ^(.*)$ http://www.qmusings.com/$1 [r=301,nc]
That should correctly send all requests to pages that were in her /blog directory to her root directory. I have not tested this code out myself but I see no reason why it should not work. The code snippet is based on the code used to redirect all traffic from example.com to www.example.com.
When I moved my blog the problem I had was that my pages looked like this:
www.hmtk.com/blog/index.php?awesome-article.html
Everything after index.php? was ignored and all pages redirected to my root. Ms. Q should not have this problem.
If this fails she can simply dump a listing of all of her URLs and import the data into a spreadsheet program (to maintain the format). Make a duplicate column of data and use a find/replace command to quickly remove the /blog from each URL. She can then add some extra columns to facilitate mass writing of the per URL redirects.
Or she can just write up some quick PHP to go into the database, pull all of the URLs and spit out a proper .htaccess file.
Step 3: Upload
Now that we have everything backed-up and our redirects are ready we can safely upload our /blog directory into our root directory. Once everything is uploaded we have to go in and change the settings in WordPress so that it knows to use the root directory from here on out. This is also the time when we add all the redirect information to the .htaccess file in the root directory.
If everything works (it should) the move will be complete.
Before deleting the old /blog directory you want to rename it and see if anything breaks. If something breaks than there is a mistake somewhere.
Step 4: Write a Post
The last thing you do is write a long post telling your readers how you just spent hours reconfiguring your blog and that it now lies at the root of the domain.
I hope this helps!