How Do I Redirect Wordpress Pages but not posts?

I have a client that changed jobs. He wants to redirect all pages on the wordpress site to a specific page on that site. But he also wants to keep all the SEO from the blog posts. So I need all the blog posts to show up, but redirect any old pages to the new page. So, for example:

oldpage.com oldpage.com/specificpage

so anything that is not a blog post goes to oldpage.com/specificpage

the blogpost would go to blogpost

I am able to redirect everything to oldpage.com/specificpage

I have excluded the homepage and /wp-admin/ etc.

But there are over 4,000 posts and to redirect each one individually is impractical Is there a way to redirect everything except the posts through .htaccess?

Topic redirect pages posts Wordpress

Category Web


You can either add the following code to your functions.php or create a custom plugin with the code.

function my_custom_page_redirect() {
  if (is_page()) {
    wp_redirect(home_url());
    exit;
  }
}
add_action('template_redirect', 'my_custom_page_redirect');

Note - change the URL in wp_redirect() to whatever the 'specificpage' is. I just used home_url() in the redirect as an example.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.