wp-config.php
if ( is_alt_domain( $_SERVER['SERVER_NAME'] ) ) {
$domain = str_replace( 'www.', '', $_SERVER['SERVER_NAME'] );
define( 'WP_SITEURL', 'http://www.' . $domain );
define( 'WP_HOME', 'http://www.' . $domain );
} else if (is_sub_domain( $_SERVER['HTTP_HOST'] ) ) {
$domain = "{$_SERVER['HTTP_HOST']}";
define( 'WP_SITEURL', 'http://' . $domain );
define( 'WP_HOME', 'http://' . $domain );
} else if (! (is_main_domain( $_SERVER['SERVER_NAME']) ) ) {
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location:http://family.com", true, 301 );
exit;
}
function is_main_domain( $domain ) {
$domain = $_SERVER['HTTP_HOST'] ;
return strcmp( $domain, my_main_domain() ) == 0 ;
}
function is_sub_domain ( $domain ) {
$domain = str_replace( my_main_domain(), '', $_SERVER['HTTP_HOST'] );
return in_array( substr( $domain, 0, -1), sub_domains() );
}
function is_alt_domain( $domain ) {
$domain = str_replace( 'www.', '', $_SERVER['SERVER_NAME'] );
$domain = substr( $domain, 0, strrpos( $domain, '.') );
return array_key_exists( $domain, alt_domains() );
}
function sub_domains() {
return array(
'me',
'her',
'kid1',
);
}
function alt_domains() {
return array(
'project1' => 'com',
'project2' => 'com',
'project3' => 'is',
);
}
function my_main_domain() {
return 'family.com';
}
functions.php
// this seems to be important
update_option('siteurl','http://' . $_SERVER["HTTP_HOST"]);
update_option('home','http://' . $_SERVER["HTTP_HOST"]);
add_action( 'category_link', 'yoursite_category_link', 11, 2 );
function yoursite_category_link( $category_link, $category_id ) {
$parts = explode( "https://wordpress.stackexchange.com/", $category_link );
// if %category% in http://%domain%/%category%/
// is in alt_domains() update the category_link
if ( array_key_exists("{$parts[3]}", alt_domains() ) ) {
$tld = alt_domains()[$parts[3]];
$category_link = "http://www.{$parts[3]}.{$tld}/"; // Strip 'category/%category%/'
}
return $category_link;
}
add_action( 'author_link', 'yoursite_author_link', 11, 2 );
function yoursite_author_link( $author_link, $author_id ) {
$parts = explode( "https://wordpress.stackexchange.com/", $author_link );
// if %author% in http://domain/author/%author%/
// is in sub_domains() update the author_link
if ( "{$parts[3]}" == "author" && in_array("{$parts[4]}", sub_domains() ) ) {
$author_link = "http://{$parts[4]}.family.com/"; // Strip 'author/%author%/'
} else {
$author_link = "http://family.com/author/{$parts[4]}"; // Strip 'author/%author%/'
}
return $author_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_alt_domain( $_SERVER['SERVER_NAME'] ) || is_sub_domain( $_SERVER['HTTP_HOST'] ) ) {
$parts = explode( "https://wordpress.stackexchange.com/", strtolower( $_SERVER['REQUEST_URI'] ) );
if ( (count($parts) > 1) && ( array_key_exists("{$parts[1]}",alt_domains() ) || ( "author" == $parts[1] && in_array("{$parts[2]}", sub_domains() ) ) ) ) {
remove_filter( 'template_redirect', 'redirect_canonical' );
}
}
}
add_action( 'template_redirect', 'yoursite_template_redirect' );
function yoursite_template_redirect() {
// Redirect any http://%domain%.%ext%/%category%/ to http://%category%.%ext%/
// and any http://%domain%.%ext%/author/%author%/ to http://%author%.%domain%.%ext%
$parts = explode( "https://wordpress.stackexchange.com/", strtolower( $_SERVER['REQUEST_URI'] ) );
if ( array_key_exists( "{$parts[1]}", alt_domains()) ) {
$tld = alt_domains()[$parts[1]];
wp_redirect( "http://www.{$parts[1]}.{$tld}/", 301 );
exit;
} else if ( 'author' == $parts[1] && in_array("{$parts[2]}", sub_domains() ) ) {
wp_redirect( "http://{$parts[2]}.famliy.com/", 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.
if ( is_alt_domain( $_SERVER['SERVER_NAME']) || is_sub_domain( $_SERVER['HTTP_HOST'] ) ) {
$category_domain = str_replace( 'www.', '', $_SERVER['SERVER_NAME'] );
$category_domain = substr( $category_domain, 0, strrpos( $category_domain, '.' ) );
$author_name = substr( $_SERVER['HTTP_HOST'], 0, strpos( $_SERVER['HTTP_HOST'], '.' ) );
$parts = explode( "https://wordpress.stackexchange.com/", $_SERVER['REQUEST_URI'] );
$category_name = "{$parts[1]}";
if ( array_key_exists($category_domain, alt_domains()) ) {
if ( strlen( $category_name ) < 1 ) {
$query_vars = array( 'pagename' => $category_domain.'-page' );
} else if ( term_exists("{$category_name}", 'category') ) {
$query_vars = array(
'category__and' => array(
get_category_by_slug($category_domain)->term_id,
get_category_by_slug($category_name)->term_id
)
);
}
} else if ( in_array( $author_name, sub_domains() ) ) {
if ( strlen( $category_name ) < 1 ) {
$query_vars = array( 'pagename' => $author_name.'-page' );
} else if (term_exists("{$category_name}", 'category') ) {
$query_vars = array(
'category_name' => $category_name,
'author_name' => $author_name
);
}
}
}
return $query_vars;
}
And, of course, you also need to point your subdomains (or *) on your nameserver to your wordpress install. You also will need to add a Virtual Host (Nginx) for any other domains you are using… and you will need to set up CORS.
As a side note, this answer used in conjuction with my next answer/question – How to have a static category/author page? – should make my intentions of having one multi-faced site a little more clear 🙂