Multiple Domain Names – One WP Install (non-Multisite) – Default Each Domain name to Category Archive

Use case is somewhat simple – here goes:

  1. Five people share a single wordpress install – not multi-user
  2. Each person blogs and puts their posts in a their own unique category
  3. Each person has their own domain name
  4. All domain names are pointed or parked to same WP install folder
  5. Each person’s domain name only shows the posts from their category, i.e.

    • http://blogger1.com would return category archive of Blogger1’s posts,
    • http://blogger2.com would return category archive of Blogger2’s posts,
    • etc.
  6. Google friendly indexing for each domain name
  7. Prefer rewrite solution rather than redirect but redirect is acceptable
  8. A”master” account would be able to post or edit any of the bloggers posts via the “main” domain login.
  9. Bonus – if this could be extended to custom post types

Environment

  1. One installation of latest version of WP (not multi-user)
  2. Pretty Permalinks
  3. Cpanel – to set up domain parking
  4. No subdomains
  5. Access to .htaccess
  6. Access to functions.php

2 Answers
2

Here’s two different solutions (editing the specifics for your use case):

Doing a Redirect:

1.) At the top of your /wp-config.php file add the following:

if ( is_yoursite_blogger_domain( $_SERVER['SERVER_NAME'] ) ) {
  $domain = str_replace( 'www.', '', $_SERVER['SERVER_NAME'] );
  define( 'WP_SITEURL', 'http://' . $domain );
  define( 'WP_HOME', 'http://' . $domain );
} else if ( ! is_main_domain( $_SERVER['SERVER_NAME'] ) ) {
  header( "Location:http://{$yoursite_main_domain}", true, 301 );
  exit;
}
function is_main_domain( $domain ) {
  $domain = str_replace( 'www.', '', $_SERVER['SERVER_NAME'] );
  return strpos( $domain, 'maindomain.com' ) !== false;
}
function is_yoursite_blogger_domain( $domain ) {
  $domain = str_replace( 'www.', '', $_SERVER['SERVER_NAME'] );
  return in_array( $domain, array(
    'blogger1.com',
    'blogger2.com',
    'blogger3.com',
    'blogger4.com',
    'blogger5.com',
    'uncategorized.dev', // Only here for use on my own test site
   ) );
}

2.) Then add this to your theme’s functions.php file:

add_action( 'template_redirect', 'yoursite_template_redirect' );
function yoursite_template_redirect() {
  $path = $_SERVER['REQUEST_URI'];
  if (strpos($path,"/category/") === false) {
    $domain = str_replace( 'www.', '', $_SERVER['SERVER_NAME'] ) ;
    $category = str_replace( '.com', '', $domain );
    $category = str_replace( '.dev', '', $domain );  // Only for my own test site
    $location = "http://{$domain}/category/{$category}/" ;
      wp_safe_redirect( $location );
      exit;
    }
  }
}

The above will do a 302 redirect to http://blogger1.com/category/blogger1/ when a request is made of any URL http://blogger1.com other than one that starts with http://blogger1.com/category/ (you made need to make some changes to support other URLs.)

Doing a “Rewrite”:

What the above does not support is “rewrite” vs. a “redirect” solution. If you want to that it is a little more complicated. The following code will result in the category page being loaded in the root path for any domain that is mapped in your is_yoursite_blogger_domain() function. Of course your is_yoursite_blogger_domain() function could be validating against existing categories but I don’t know the full criteria so I just hard coded it. You can copy this code into your function’s theme.php file or put into a .php file of a plugin you might be writing. Note that this code also requires the code in the /wp-config.php file above:

add_action( 'category_link', 'yoursite_category_link', 11, 2 );
function yoursite_category_link( $category_link, $category_id ) {
  // Make sure any blogger category links are truncated to the root
  $parts = explode( "https://wordpress.stackexchange.com/", $category_link );
  if ( is_yoursite_blogger_domain( $parts[2] ) ) {
    // if %category% in http://%domain%/category/%category%/ 
    // equals %domain% minus the 'com' extension
    if ( "{$parts[4]}." == substr( $parts[2], 0, strlen( $parts[4] ) + 1 ) ) {
     $category_link = "http://{$parts[2]}/"; // Strip 'category/%category%/'
    }
  }
  return $category_link;
}

add_action( 'init', 'yoursite_init' );
function yoursite_init() {
  // Remove the canonical redirect to the category page
  // if %category% in http://%category%.%ext%/ is a blogger category.
  if ( is_yoursite_blogger_domain( $_SERVER['SERVER_NAME'] ) ) {
    $parts = explode( "https://wordpress.stackexchange.com/", strtolower( $_SERVER['REQUEST_URI'] ) );
    if (count($parts) > 1) {
      $category_base = get_option( 'category_base' );
      if ( empty( $category_base ) )
        $category_base="category";
      if ( $category_base == $parts[1] ) {
        remove_filter( 'template_redirect', 'redirect_canonical' );
      }
    }
  }
}

add_action( 'template_redirect', 'yoursite_template_redirect' );
function yoursite_template_redirect() {
  // Redirect any http://%category%.%ext%/category/%category%/ to http://%category%.%ext%/
  if ( is_yoursite_blogger_domain( $_SERVER['SERVER_NAME'] ) ) {
    $category_base = get_option('category_base');
    if (empty($category_base))
      $category_base="category";
    $parts = explode( "https://wordpress.stackexchange.com/", strtolower( $_SERVER['REQUEST_URI'] ) );
    if ( $category_base == $parts[1] &&
       "{$parts[2]}." == substr( $_SERVER['SERVER_NAME'], 0, strlen( $parts[2] ) + 1 ) ) {
      wp_safe_redirect( "http://{$_SERVER['SERVER_NAME']}/", 301 );
      exit;
    }
  }
}

add_action( 'request', 'yoursite_request' );
function yoursite_request($query_vars) {
  // If %category% in http://%category%.%ext%/ is a blogger category set the
  // 'category_name' query var to tell WordPress to display the category page.
  $domain = $_SERVER['SERVER_NAME'];
  if ( is_yoursite_blogger_domain( $domain ) ) {
    $path = $_SERVER['REQUEST_URI'];
    if ( strpos( $path, "/category/" ) === false ) {
      $domain = str_replace( 'www.', '', $domain ) ;
      $category_name = substr( $domain, 0, strrpos( $domain, '.' ) );
      $query_vars = array( 'category_name' => $category_name );
    }
  }
  return $query_vars;
}

And here’s a screen shot to show the second example in action:

Screenshot of a WordPress site using hooks to enable a category page to be displayed in the URL root for selected categories
(source: mikeschinkel.com)

Another probably better way to approach this would be with a custom taxonomy instead of categories, or even better yet, map the domain to a user name instead. That way it’s less work on the authors and you don’t have to worry about maintaining the blogger list in code or in a separate taxonomy list.

As for you “bonus”, sorry but I don’t follow what you want there.

Leave a Comment