301 redirect in .htaccess - old URL includes ?page_id=

301 redirect in .htaccess - old URL includes ?page_id= - 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 301 redirect in .htaccess - old URL includes ?page_id= error on your web browser. Problem :


I want to 301 redirect several URLs from an old WordPress installation within one subdirectory to a new WP installation within a different directory of the same domain. I.E.:



http://www.example.de/abc/?page_id=123


needs to go to:



http://www.example.de/xyz/newindividualpage/


I have tried several options I found here and elsewhere, but so far I was unable to get it to work, resp. adapt found code correctly to my needs. Obviously the ?page_id= structure is where I am missing it.


Solution :

In order to match the query string (ie. page_id=123 part) you need a RewriteCond directive and the QUERY_STRING server variable (from mod_rewrite). Try something like the following in the /abc/.htaccess file (ie. in the subdirectory you want to redirect from):



RewriteEngine On
RewriteCond %QUERY_STRING ^page_id=123
RewriteRule ^$ /xyz/newindividualpage/ [R=302,L]


(Change the 302 to 301 when you are sure it's working OK. 302 (temporary) redirects aren't cached by the browser, so makes testing easier.)



This redirects any URL within the /abc subdirectory that has a query string that starts page_id=123 (as per your example).






Alternatively, if you prefer to only use the .htaccess file in the document root, then add the following to the top of the .htaccess file (at least before any other mod_rewrite directives) in the root directory:



RewriteEngine On
RewriteCond %QUERY_STRING ^page_id=123
RewriteRule ^abc/?$ /xyz/newindividualpage/ [R=301,L]


The RewriteEngine directive only needs to appear once in the file. It can appear anywhere in the file, although it is more logical if it appears at the top. (It doesn't matter if it occurs more than once, it's just unnecessary.)



#Options +FollowSymlinks
RewriteEngine on
rewritecond %http_host ^example.com [nc]
rewriterule ^(.*)$ example.com/$1 [r=301,nc]


You can also migrate your Wordpress posts using Wordpress Plugins wpclone by wp academy.



You could also just install the excellent WP Redirect plugin that will make for a quick fix.


We hope that this article has helped you resolve the htaccess, 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=