301 htaccess Redirect: Changing WordPress Installation Off Root to Sub-Folder, Keeping Permalinks, SEO

This is perhaps the most messed up title of a blog post ever but I’m attempting to capitalize on keywords that I used to help solve my challenge this morning. And yes, it took me quite a while to keep the character count low…!

I can’t imagine that I’m the only one with this challenge and you might have to do this at some point in the future so this blog post might be worth a bookmark for future use.

Here was my challenge:

  • I installed WordPress on the root directory of a domain and wanted to move it to a sub-folder completely on the same server.
  • It wasn’t about combining blogs, migrating platforms, or like moving a .com to a .org.
  • I wanted to create a small landing page on the root instead and have the blog content redirected to a /blog instead, keeping permalinks in-tact and making sure that the right redirects made it to the new structure (e.g. www.blog.com/blog-post ~~~> www.blog.com/blog/blog-post)
  • Keep as much SEO as possible as noted by the new structure.

I’m not a super-ninja when it comes to .htaccess and it’s not worth memorizing since most needs are just a Google search away, but this was a bit different and I didn’t find anything immediately that could help me. So, I ended up combining a few calls and it worked.

Here’s what I did:

1. Migrate WordPress as Usual

Follow a few of these steps here and you can get most of it done. I’ve done this so many times that this was fine and did this in a few seconds.

  • Create a new folder off the root, called /blog
  • Change Site Address URL in your Settings
  • Move files into new folder
  • Edit permalink settings in Settings
  • Edit wp-config.php if you needed

Done and done.

2. Upload New Landing Page to Root

I was making a small update to Action & Influence‘s website, as you can see here:

action-influence

After uploading was where I realized that I needed a different type of .htaccess file to make sure that the blog posts that were sitting at the root now needed to be redirected to /blog while keeping this new homepage accessible as well.

3. Setup .htaccess

Check out the following code that I used:

[cc]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/index.html$
#Anything else you want to load directly above and below is for the blog and dynamic calls
RewriteRule (.*) http://www.myai.org/blog/$1 [R=301,L]
[/cc]

In layman’s terms, you can see I’m asking it to show the homepage at http://myai.org/ (and a few other direct pages) and then as adding a /blog/ to anything else that’s being requested for WordPress-related blog posts and pages.

For example http://myai.org/introvert-extrovert-leader/ goes to http://myai.org/blog/introvert-extrovert-leader/

And that’s that. What I learned was that you could combine some requests for direct access pages and for dynamic as well.

You learn something new every day.

If you have any questions just hit me up in the comments.

Major kudos to StackOverflow. For serious.

  • http://mark.mcwilliams.me/ Mark McWilliams

    I’m surprised you didn’t just decide to use a built-in feature of WordPress for displaying a different landing page, then creating a separate page for the blog? — On the plus side, it’d have kept the whole site running WordPress, and showcase something else Standard could do! :)

    • http://twitter.com/zimbrul zimbrul (@zimbrul)

      I was thinking the same from the beginning of the article. Why don’t just use a page as your landing home page and have the blog moved to /blog?

  • Djchamike

    Wow, that makes sense, and seems so apparent — yet not me at the time!
    And logical if you think of process… so send all the files on over to the new site, and then deal with those pages that aren’t the same with similar rules on new site. But all those blog posts with the same page names can be handled by the one rule on old site.

  • http://www.mrpinindia.in Pranjal Gupta

    Hi, I need a way to redirect entire sub directory to root directory!

    It’s like, http://www.mysite.com/subdirectory to http://www.mysite.com

    Can you please let me know any solution?

    Thanks