5

I'd like to use blog.example.com as the base for my blog posts, e.g. http://blog.example.com/2013/08/29/sample-post/ and www.example.com for my pages, e.g. www.example.com/about-me/

Is there an easy way to do this? I tried with creating a network but that gives me two WordPress installations, both with posts and pages and their own admin dashboards. I just want to go posts to one subdomain, pages to the other.

  • Tbh, I don't think this is possible. You need two installations of WP, basically. You'd have two admin consoles of course. – Athoxx Dec 15 '16 at 14:48
  • My question is: Why do you want this? – Michael Ecklund Dec 16 '16 at 15:34
  • well, mainly because I like it better that way. But also so that I can create landing pages with on different subdomains. At least that was my reason for asking this question more than 3 years ago ;-) – Florian Heinle Dec 16 '16 at 19:57
7
+50

I might have a solution for you.

First steps:

  1. Within your domain's DNS manager you need to create a CNAME record. The name would be blog and the hostname would be your domain name. This is necessary because the browser needs to know where to point the subdomain blog.mydomain.com to;
  2. Second, you need to tell apache (I suppose you are using apache) where is the subdomain blog.mydomain.com's directory location. This is necessary because once the browser knows in which server to find your subdomain, then it needs apache to tell it in which folder to look at.

Once you are done with the first steps maybe you will have to wait for at least 15 minutes in order to be sure your changes were all propagated.

If you just want your single posts to be accessed via blog.mydomain.com/xxxx/xx/xx/my-post you are done. But, be warned that any page within your site will be accessed via blog.mydomain.com/some-page. For example, the user will be able to access both mydomain.com/my-contact-page and blog.mydomain.com/my-contact-page.

Besides, the changing to domain blog.mydomain.com is not being forced. If you want it to be forced you need to edit your .htaccess file and insert before the wordpress tag the following:

# BEGIN custom redirect
# You might want to change 'blog' to your current blog page slug or to sth else you want!
<IfModule mod_rewrite.c>
# Here we are making sure when the user accesses blog.mydomain.com he will see the blog page content.
RewriteCond %{HTTP_HOST} ^blog\.
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule ^.*$ index.php?pagename=blog [L]

# Here we are redirecting mydomain.com/2016/12/16/my-post to blog.mydomain.com/2016/12/16/my-post
RewriteCond %{HTTP_HOST} !^blog\.
RewriteRule ^([0-9]{4})\/([0-9]{1,2})\/([0-9]{1,2})\/(.+)\/?$ http://blog.mydomain.com/$1/$2/$3/$4 [L,NC]

# Here we are redirecting mydomain.com/blog to blog.mydomain.com
RewriteRule ^blog\/?$ http://blog.mydomain.com [L,NC]
</IfModule>

# END custom redirect

Pay attention to the comments along the code snippet in order to perform your own changes in case you need to.

If you want to prevent another pages from being seen through the subdomain you can add this code snippet in your functions.php file:


function push_redirect() {

    $host = $_SERVER['HTTP_HOST'];

    // If we are not seeing the subdomain we have no reasons to continue.
    if ( !preg_match( "/^blog\./", $host ) )
        return;

    // If we are seeing blog homepage or single post we have no reasons to continue.
    if ( is_home() || is_singular( 'post' ) )
        return;

    $site_url = site_url();
    $uri = $_SERVER['REQUEST_URI'];
    $redirect_to = $site_url . $uri;

    wp_redirect( $redirect_to, 302 );

    exit;

}

add_action( 'template_redirect', 'push_redirect', 10 );

I hope this is what you were looking for.

1

I believe the only way is to use mod_rewrite in your .htaccess to turn a fixed url; say http://www.example.com/blog into http://blog.example.com

  1. Prefix all your articles with "blog".

  2. Place this in .htaccess inside your /blog directory

:

RewriteEngine on
RewriteCond ${HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*) http://blog.domain.com/$1 [R,L]
  • 2
    wordpress canonical redirect will cause an endless redirect loop. This rule at best is a starting point but far from being a solution – Mark Kaplun Dec 14 '16 at 6:55
  • Also, note that I don't have multiple instalations; just one.. so all files in same dir ;) – Toni Michel Caubet Dec 14 '16 at 11:48
  • You can also do the redirect from the root directory, the htaccess code would just be a little different. – Pim Dec 14 '16 at 12:06
  • Anyways, @MarkKaplun is right; this would produce in infinite redirection.. anyways.. thanks for your time! – Toni Michel Caubet Dec 14 '16 at 12:15
  • 1
    RewriteRule is a server side directive. You don't need to use a 30X redirect with it. You certainly could craft a rule that serves example.com/2013/08/29/sample-post when you call blog.example.com/2013/08/29/sample-post. – Landing on Jupiter Dec 14 '16 at 13:52

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.