WordPress mod_rewrite redirect specific folders

WordPress mod_rewrite redirect specific folders - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the WordPress mod_rewrite redirect specific folders error on your web browser. Problem :


System: Debian Etch, Apache 2.2



I have a WordPress instance with multiple blogs. I would like to redirect some of the folders based on the year and month, while leaving other folders to go to the actual locations.



Example: I have archives for a few years, like 2010, 2011 and 2012:




  • http://example.com/wordpress/myblog/2010/02

  • http://example.com/wordpress/myblog/2011/01

  • http://example.com/wordpress/myblog/2012/01



I would like to redirect all 2010 and 2011 posts to another blog with the same folder structure:




  • http://example.com/wordpress/myotherblog/2010/02

  • http://example.com/wordpress/myotherblog/2011/01

  • and so on.



I would like to have 2012 and beyond to go to the actual site (ie. http://example.com/wordpress/myblog/2012/01).



I tried mod_rewrite with the following, one rule at a time to test redirection for just one year (and to expand later for other years), and none of them worked!




  • RewriteEngine is already on since there are some default WordPress rewrites.

  • RewriteBase is set to http://example.com/wordpress/.

  • I put my rule before all the other default WordPress rules are processed.



Didn't work solution #1



RedirectMatch 301 /myblog/2010/(.*) /myotherblog/2010/$1


Didn't work solution #2



RewriteRule /myblog/2010/(.*) http://example.com/myotherblog/2010/$1 [R=301]


Didn't work solution #3



RedirectPermanent /myblog/2010/(.*) http://example.com/myotherblog/2010/$1


I've also tried the above rules with and without a fully qualified URL for the new location.



The rewrite log, with log level set to 9, did not provide any useful information. It shows that it looks at the pattern specified against the URL (as mentioned in the rule), but finally what happens is a passthrough to http://example.com/myblog/ for all URLs or a 500 Internal Server Error.



Any ideas on where I could be going wrong or any alternative solutions?


Solution :

I can't explain why your rules don't work without seeing the whole .htaccess file, but you might find the Monkeyman Rewrite Analyzer Plugin helpful:-




It is only an analyzer, it does not change any rules for you. It
parses the rules down to their components and shows the connection
with the resulting query variables. It allows you to try out different
URLs to see which rules will match and what the value of the different
query variables will be.




This might give you some better clues on how to debug your code.



Try the following RewriteRule:



RewriteRule ^myblog/(2010|2011)/(.*)$ http://example.com/myotherblog/$1/$2 [R=301,L,NC]


Notice the inclusion of the L flag (last directive) the NC flag for nocase and the fact that the path name doesn't include the forward slash (since you might be dropping a .htaccess file into a directory). If it is not in a .htaccess file in a directory path that doesn't include the forward slash then simply:



RewriteRule ^/myblog/(2010|2011)/(.*)$ http://example.com/myotherblog/$1/$2 [R=301,L,NC]


If you are not sure of either then



RewriteRule myblog/(2010|2011)/(.*)$ http://example.com/myotherblog/$1/$2 [R=301,L,NC]

We hope that this article has helped you resolve the apache, wordpress, redirects error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

How to redirect to any domain [duplicate]

"302 found" for index page bad for SEO?

Create redirect from url like www.example.us/?p=100&option=