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.:


needs to go to:


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.


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.)

  • Thank you so much, w3dk! I had no idea that I have to put it into the subdirectory instead of the root directory! Jun 17 '16 at 16:23
  • 1
    Well, it doesn't have to go in the subdirectory, but it makes it a bit easier. (If it went in the root/parent .htaccess file you would need to change it slightly and ensure it doesn't conflict with existing mod_rewrite directives.)
    – MrWhite
    Jun 17 '16 at 16:42
  • w3dk, I am happy the solution works! But if there is a way to move it into the root directory I would eventually have the chance to completely delete the subdirectory once I do not need it anymore. Jun 18 '16 at 8:24
  • I've updated my answer with alternative code for the .htaccess file in the root directory.
    – MrWhite
    Jun 19 '16 at 10:22
  • 1
    w3dk - this it just perfect and made my day! Jun 20 '16 at 9:30
#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.

  • No worries, all is fine since late June of last year, with w3dk's solution. Thanks for the effort though! Feb 18 '17 at 3:53

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

  • 3
    Link only answers are not allowed. You need to provide context for the link that supplies an answer. Your linked reference can disappear tomorrow doing no one any good.
    – Rob
    Jun 18 '16 at 1:29
  • Isn't a plugin a bit overkill for such a redirect?
    – MrWhite
    Jun 19 '16 at 10:27
  • The old installation is too old for that plugin, Tom. Also, I want to get rid of the old installation and thus prefer the solution provided by w3dk. Jun 20 '16 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.