I have a b blog of over 200k+ post and I moved all post from a specific category to a subcategory, I have also deleted the post from the my main site, so all the post of that category have been moved to a subcategory.

Our analytics show that a lot of crawling errors, and my wp permalink structure for main site is example.com/postname/, while the one for my sub-category is example.com/stack/postname/

The solution I came up with is to redirect all visits for non-existant URLs of main site to subcategory URLs


  • mysite.com/postname/ which does not exist will be redirected to my sub-category url
  • example.com/stack/postname/ this pages are active and have content.

How would I go about this?

  • See the answer here stackoverflow.com/questions/19962787/… . Example shown in that answer. Commented Jun 5, 2019 at 19:28
  • That does not fix my issue, because I don't have 404 error page at my public_html, wp theme function would be the best recommendation.
    – XATA
    Commented Jun 6, 2019 at 14:01
  • If you use that link, you can see how to redirect a '404' response to another page. You provide the page you want to show in the command. You don't need a 404 page, you just need the 'errordocument' command to 'catch' a 404 response and send it to the page/link you desire. Commented Jun 6, 2019 at 18:08
  • its not working.check my post, I did not mention specific url, I mention urls that match the same url i deleted
    – XATA
    Commented Jun 6, 2019 at 19:41
  • errordocument 404 /your-subcatetory-page-without-domain-name . Place the lines above the WP htaccess lines. Use the actual page you want to redirect to if a page is not found. If you want to do some WP logic to a specific page based on some condition, then create/use a '404' template. Also, see wordpress.stackexchange.com/questions/268669/… Commented Jun 6, 2019 at 20:01

1 Answer 1


header("HTTP/1.1 301 Moved Permanently");

header("Location: https://{$_SERVER['SERVER_NAME']}/subcategory{$_SERVER['REQUEST_URI']}"); 



The above code was added directly to themes 404.php and everything worked as expected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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