How do I redirect thousands of WordPress URLs to a subdirectory?

How do I redirect thousands of WordPress URLs to a subdirectory? - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about seo, domains, htaccess, wordpress, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


I have a WordPress website and it has almost 22,000 blog posts. It has been running with good SEO for the past 6 years. The contents are scattered in almost all social media and backlinked in a lot of websites. I also have data in Google Analytics that I would like to preserve.



I'm planning to start a fresh news aggregator site at the base directory of the domain. I want to move the old blog contents to the subdirectory i.e. /blog. I want to run the same old blog in it without affecting the SEO.



I tried implementing 301 redirects in .htaccess, but they are not working the way I want. This is my blog URL pattern /2015/01/10/post-title/ and I tried with this 301 redirection code in .htaccess:



Redirect 301 / http://www.example.com/blog


However, it is redirecting all URLs of the aggregator site which I'm currently running under www directory.


Solution :


This is my blog URL pattern /2015/01/10/post-title/



You need to specifically check for the old URL pattern, otherwise you will naturally redirect everything, including all the new URLs of the "news aggregator site".


Try, something like the following instead, at the top of your .htaccess file:


RewriteEngine On

RewriteRule ^d4/dd/dd/[w-]+/$ /blog/$0 [R=301,L]

This specifically checks for URLs of the form /2015/01/10/post-title/ and 301 redirects to /blog/2015/01/10/post-title/.


$0 is a backreference that contains the entire URL-path that is matched by the RewriteRule pattern.


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect