Redirect posts to sub domain except pages

admin2025-01-08  3

We have a WordPress site, which contains posts and pages. Now we moved posts into sub domain but we kept pages into root domain. Only Posts has .html extension and there is no difference in the URL for Posts & Pages.

Sample Post: www.mydomain/sample-posts.html need to redirect with blog.mydomain/sample-posts.html.

Sample Page: www.mydomain/sample-pages

How to redirect only posts in to sub domain?

We have a WordPress site, which contains posts and pages. Now we moved posts into sub domain but we kept pages into root domain. Only Posts has .html extension and there is no difference in the URL for Posts & Pages.

Sample Post: www.mydomain.com/sample-posts.html need to redirect with blog.mydomain.com/sample-posts.html.

Sample Page: www.mydomain.com/sample-pages

How to redirect only posts in to sub domain?

Share Improve this question edited Dec 28, 2012 at 13:25 CommunityBot 1 asked Dec 26, 2012 at 10:27 palPalanipalPalani 763 bronze badges 1
  • It might matter how did you do it? Is this still one WP installation? – Rarst Commented Dec 26, 2012 at 12:26
Add a comment  | 

1 Answer 1

Reset to default 0

I would do this per server configuration. Here is an example for the .htaccess:

RewriteCond %{HTTP_HOST} !^blog\.example\.com$
RewriteRule (.*)\.html$ http://blog.example.com/$1.html [L,R=301]

You could do this in a WordPress plugin too. Untested suggestion:

add_action( 'template_redirect', 'wpse_77279_redirect' );

function wpse_77279_redirect()
{
    if ( is_single() 
        and 'blog.example.com' !== $_SERVER['HTTP_HOST'] 
        and 'post' === get_post_type() 
    )
    {
        wp_redirect( 'blog.example.com' . $_SERVER['REQUEST_URI'] );
        exit;
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736268268a1261.html

最新回复(0)