Bulk .htaccess Redirect: Move WordPress from Subfolder

Updated on: December 19, 2024

I recently had to move a WordPress blog from a subfolder named “/blog/” to the root of the web server and wanted to use htaccess redirect rules to handle the process. The blog portion of the website used to be in its own folder and separate from the main website.

I rebuilt the entire website with a new WordPress install and imported all the posts. This was causing all sorts of 301 errors, and all external links to the website were now landing on a 404 page.

Basically, all parts of the blog (categories, tags, posts, archives) needed to be redirected from:

http://www.website.com/blog/categories-pages-tags-archives/

to:

http://www.website.com/categories-pages-tags-archives/

Rather than create a 301 Redirect for every single tag, category, archive, and post, I researched a way for my htaccess redirect to do it. This ended up being harder to find than you might expect. I finally stumbled across this code and integrated it with the default WordPress .htaccess script:

rewriterule ^blog(/.*)? $1 [L,R=301]

With the WordPress code, it looks like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^blog(/.*)? $1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

This approach ensures all parts of the WordPress blog are seamlessly redirected from the subfolder to the root, maintaining link integrity and preventing 404 errors.

Using the Redirection Plugin for Bulk Htaccess Redirect

If you’re not comfortable working with .htaccess or prefer a more user-friendly option, using a plugin like Redirection can make this process much simpler. The plugin allows you to set up wildcard redirects with minimal effort, right from the WordPress dashboard.

Here’s how you can do it:

  1. Install and activate the Redirection plugin.
    Navigate to Plugins > Add New, search for “Redirection,” and install it. Once activated, go to Tools > Redirection to access its settings.
  2. Set up a wildcard redirect:
    In the Redirection settings, create a new redirect rule:

    • Source URL:
      ^/blog/(.*)
    • Target URL:
      /$1

    Check the Regex box to enable wildcard matching.

  3. Save the rule, and the plugin will handle all redirects from the subfolder /blog/ to the root automatically.

This method is straightforward and removes the need to manually edit .htaccess. Plus, the plugin logs all redirects, so you can track how often they’re used, which can be helpful for cleanup later.

Whether you go with the .htaccess approach or the plugin, both options will ensure your WordPress migration is seamless and free from 404 errors.