301 Redirect only Posts not Pages

I am looking to redirect all the posts and not pages and custom post types from old domain to new domain. So all posts for instance
http://www.example.com/post1 should go to http://example1.com/post1
http://www.example.com/post1 should go to http://example1.com/post1

The pages i.e. http://www.example.com/page1 should still be functional

Read More

I can find alot of answers on moving blog into sub directory along with moving all the links but not for moving just the posts and not the pages

Edit
There are over 1000 posts so I am looking for a dynamic way of doing this

Thank you for your help

Related posts

Leave a Reply

2 comments

  1. The hack I finally end up using was

    1. Add ob_start to single.php as first line even before get_header()
    2. Add following code in functions.php

    change example.com with your site domain name

    add_action('wp_head','redirectme');
    function redirectme() {
        if ('post' === get_post_type()){
            wp_redirect( 'http://example.com' . $_SERVER['REQUEST_URI'] , 301 ); exit;
        }
    }
    
  2. I don´t think this is possible with this URL schema. .htaccess (or the Apache behind it) doesn´t know if post1 is a post or page. How shall it then redirect on this base?

    If you would change the URL structure for pages alone (I don´t think this is possible by default, or am I wrong?) you could target their URL scheme and redirect.

    To access the old pages (with the new URL) you could then use a “broken link checker” plugin.

    Update: You could possibly use the hook described here to check wether the called post is a post or page and then use wp_redirect to call your new URL.