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

Use case is somewhat simple- here goes:Five people share a single wordpress install - not multi-user Each person blog

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.

    • would return category archive of Blogger1's posts,
    • 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

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 would return category archive of Blogger1's posts,
    • http://blogger2 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
Share Improve this question edited Apr 18, 2011 at 3:35 aj martin asked Apr 15, 2011 at 15:52 aj martinaj martin 532 silver badges8 bronze badges 2
  • Why wouldn't you just use Multisite for this? – Michael Ecklund Commented Mar 30, 2017 at 16:59
  • This is functionally equivalent to multisite, but more complicated, and the master account would just be a super admin user. As an aside, the domains here you'll want rewriting, but even then, permalink rewrite rules won't be able to handle this scenario, doing this will be fragile, take a lot of time to maintain, easily break, and be difficult to implement. Multisite on the other hand does all of this by design – Tom J Nowell Commented Mar 30, 2017 at 17:54
Add a comment  | 

2 Answers 2

Reset to default 5

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' ) !== false;
}
function is_yoursite_blogger_domain( $domain ) {
  $domain = str_replace( 'www.', '', $_SERVER['SERVER_NAME'] );
  return in_array( $domain, array(
    'blogger1',
    'blogger2',
    'blogger3',
    'blogger4',
    'blogger5',
    '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( '', '', $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/category/blogger1/ when a request is made of any URL http://blogger1 other than one that starts with http://blogger1/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( '/', $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( '/', 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( '/', 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:


(source: mikeschinkel)

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.

  1. Create the domain alias(es)

  2. Add this to your wp-config file:

    define('WP_SITEURL','http://'.$_SERVER['SERVER_NAME']);
    define('WP_HOME','http://'.$_SERVER['SERVER_NAME']);
    
  3. Go to your theme and add something like this to load the main content (used this for a one pager):

    $home=get_page_by_path(substr($_SERVER['SERVER_NAME'], 0, strlen($_SERVER['SERVER_NAME'])-4));
    echo do_shortcode($home->post_content);
    
  4. change the categorie, page or post slug to the domain name min the toplevel domain, in this case a toplevel domain only with 4 characters. For example: if you want to use something like .nl .be or .de than you should change the 4 into a 3. If you wanna use both than you've to add conditions.

Note: This may only work for one page, but I'm only giving you the idea. You can expand the functionality of the code yourself if it's necessary

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745462456a4628770.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信