Using .htaccess to 301 redirect WordPress URLs to the Root

I recently had to move a WordPress blog from a subfolder named “/blog/” to the root of the web server, the blog portion of the website used to be in its own folder and seperate 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/

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

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

With the Worpdress code, it looks like so.

# 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