How to use a sub domains in WordPress

What is a sub domain?

If we take a web address like http://wordpress.passiveincomereality.com, it is basically a hierarchy.

  1. com – Level 1 or top of your domain hierarchy.
  2. passiveincomereality – Level 2 is your domain.
  3. wordpress – Level 3, this is what is called a sub domain. These are optional, by default all websites have WWW setup, but this can be left off the web address on your browser and it will still find the root domain.

A sub domain is any element of a URL or domain which is level 3 or greater in the hierarchy, they are used to divide large websites into sections. An alternative method used to achieve this is to use directories, WordPress uses this structure with its wp-admin directory for admin access. Unfortunately I don’t know of a way to get WordPress to configure your site into a way which this can be utilised, well not with out some effort. But essentially how most non-Wordpress sites are setup is that a sub domain will point to a directory off the main site.

The other main benefit of sub domains are that they can be used with your web email. For example if your site was setup with a sales and support sub domain you could then have bob@sales.yoursite.com, as well as bob@support.yoursite.com, with each with its own email box. Useful no?

How to create a sub domain?

This depends somewhat on your hosting. I shall detail how it gets setup using 1&1′s hosting, as it’s the only hosting account I have access too.

First off sign into the 1&1 administrator page. From here click on domains.

Sub domain set up

Clicking on ‘New’ and selecting ‘Create Subdomain’ form the menu, this will open up a page ‘Create Subdomain’. All you need to do is to check the drop down on the right is the correct domain, you have have more then one registered, then add the name of the sub domain in the Domain text box. In this case the name will be the same as the permalink of the post or page. Pressing OK will get the sub domain setup, and you will go back to your previous page.

naming a sub domain

The sub domain is initially setup to point to your domain’s site root. This is fine for the method we are going to employ to get sub domains working with wordpress, but some web sites will want to point this sub domain at a sub directory, if you need to change it, it is covered in this post ‘Configure your domain to point at your web site‘. You sub domain will take 5 to 10 minutes before it’s ready.creating a sub domain

How to make a sub domain in WordPress?

To get sub domains to work in WordPress I found a neat plugin called ‘Automatic Subdomains’, it can be found and installed from the WordPress Admin dashboard ‘Add New’ menu under ‘Plugins’ as in this post on Plugins. You can also find the plugins home page here http://wordpress.org/extend/plugins/automatic-subdomains.

The Plugin works by checking if your site has been accessed by a URL containing a sub domain. If it was, that it will check to see if there is a page or post with the same permalinks if there is it redirect the browser to that page. If there is not, you get to see the normal front page. As in my example sub domain wordpress.passiveincomereality.com redirects to a page called wordpress.

How to get a sub domain to point to an authors page in WordPress

I needing some extra functionality for another web site I was working on. The web site needed to allow WordPress users to access their own page using a sub domain.

Example:

Instead of having for them to use http://www.yoursite.com/author/name it can now use http://name.yoursite.com

While I wait for the original author (Thanks a bunch for creating and sharing the plug in in the first place) to see if he/she wants to update the code with either the code below or add his/her own, I’ll post it below in case anyone else finds it useful and needs the functionality.

If you do want to use it, do the following:

  1. Install and activate the plugin ‘Automatic Subdomains’.
  2. Copy the code below.
  3. Go into ‘Plugins’ menu and select ‘Installed Plugins’.
  4. On the Plugin labelled  ‘Automatic Subdomains’ select ‘edit’.
  5. Delete all the code in the box, and replace it with the code you copied in from below.
  6. Now click on ‘update file’ and your plugin should now re-direct authors.

Do let me know if you have any problems with getting users to work.

Copy this code below:

<?php

/*
Plugin Name: Automatic Subdomains
Plugin URI: http://www.thejakegroup.com/wordpress/
Description: Automatically maps subdomains to page and post permalinks.
Version: 1.2
Author: Tyler Bruffy & Mark Wahl
Author URI: http://www.thejakegroup.com
License: GPL2
*/

/* Copyright 2012 Mark Wahl (email : markwahl99@yahoo.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

// Add the function tjg_as_get_subdomain when wordpress is initialized.
add_action(‘init’,’tjg_as_get_subdomain’);

function tjg_as_get_subdomain()
{
// Get the URL that was entered
$URL = $_SERVER['HTTP_HOST'];
// Extract the subdomain component
$fulldomain = explode(‘.’,$URL);
$subdomain = $fulldomain[0];
// Check whether there was a subdomain
$domaindepth = count($fulldomain);
// Get the siteURl to check against
$siteurl = site_url();
$strippedURL = str_replace(‘http://’,”,$siteurl);
$editedSiteurl = explode(‘.’,$strippedURL);
// If there is a subdomain, and that subdomain isn’t the siteURL
if ($domaindepth > 2 AND $fulldomain != $editedSiteurl) {
// Run a SQL query on the WordPress DB for posts where
// the subdomain is the same as the post_name.
global $wpdb;
$object = $wpdb->get_results(
“SELECT * FROM $wpdb->posts WHERE post_name = ‘$subdomain’”);

// If the query had a result
if (!empty($object))
{
// Get the post Object with that ID
$post = get_post($object[0]->ID);
// Get the permalink for that post
$permalink = get_permalink($post);
// And take us there
header(“Location: $permalink”);
return;
}

$object = $wpdb->get_results(“SELECT * FROM $wpdb->users WHERE user_login = ‘$subdomain’”);
// If the query had a result
if (!empty($object))
{
header(“Location: $siteurl//author//$subdomain”);
return;
}

// Otherwise, go to the homepage
else {
header(“Location: $siteurl”);
}
}
}

?>