Using Session Variables and Domain Aliases to Change the WordPress URL

Alright, so I ran into an interesting situation the other day.  Here's the scenario... A client of mine needed a website redesign that basically combined two sites into one.  She had two almost IDENTICAL sites that only differed slightly.  Aside from a couple of different pages and some price changes on the courses she offered, the sites were the same.  No need for two sites now is there?  Of course not!  The tricky part was that each site had a different URL.  If a user entered the site from www.mysiteA.com they'd see one thing.  If they entered the site from www.mysiteB.com they'd see another thing.  The challenge was getting WordPress to rewrite the URLs based on the aforementioned criteria.  After thinking about this and bugging SKC, here's what I came up with...

Step 1: Editing the wp.config file

All that needs to happen here is that this code goes into the top of the wp-config.php file.  Obviously you'll have to replace the mysiteA.com, mysiteB.com and session variables with values of you choice but that's still pretty simple.  Let's check it out.

session_start();
$url_host = $_SERVER['HTTP_HOST'];
if (!isset($_SESSION['domainReferrer']))
if ($url_host == 'mysiteA.com' || $url_host == 'www.mysiteA.com') {
$_SESSION['domainReferrer'] = 'Site A';
define('WP_SITEURL', 'http://mysiteA.com');
define('WP_HOME', 'http://mysiteA.com');
} else {
$_SESSION['domainReferrer'] = 'Site B';
define('WP_SITEURL', 'http://mysiteB.com');
define('WP_HOME', 'http://mysiteB.com');
}
else {
if ($url_host == 'mysiteA.com' || $url_host == 'www.mysiteA.com'){
$_SESSION['domainReferrer'] = 'Site A';
define('WP_SITEURL', 'http://mysiteA.com');
define('WP_HOME', 'http://mysiteA.com');
} else {
$_SESSION['domainReferrer'] = 'Site B';
define('WP_SITEURL', 'http://mysiteB.com');
define('WP_HOME', 'http://mysiteB.com');
}
}

Step 2: Conditionally Controlling Presentation / Functionality

In almost every situation, my code looked something like the code below to conditionally control what was being display.


if ($_SESSION['domainReferrer']=='Site A') {
// Put Site A Logic Here
} else {
// Put Site B Logic Here
}

Step 3: Configuring Your Server's Virtual Hosts

This part is going to be different for everyone but basically the idea is to have a main mysiteA.com as the virtual host that has PHYSICAL hosting on your server and then to map mysiteB.com as a domain alias of mysiteA.com.  The good thing though is that it doesn't matter which site (either mysiteA.com or mysiteB.com) has the physical hosting and which is the domain alias.  This is because they are pretty much the same thing.

Well, that's about it!!!  I hope this little hack comes in useful for someone!!

Related Posts:

This entry was posted in PHP Development, Web Design & Development, Wordpress and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

5 Comments

  1. Posted March 15, 2011 at 7:38 am | Permalink

    That’s pretty impressive, Chris. Could you further break down session variables based upon time of day?

    - 8am-12pm = http://www.mysiteA.com/111/
    - 12pm-4pm = http://www.myssiteA.com/222/

  2. Jason Cramer
    Posted March 14, 2011 at 6:33 pm | Permalink

    I had thought about doing something like this for some record label sites since one was going away and the other was just going to use the same site as the old one. This would have come in handy if the domain would’ve stayed pointing to my hosting, then I could have done this trick and changed everything for the new label, but still could have kept the old label’s site in place. Too bad we didn’t know about this.

    Nice work on this man.

  3. Posted March 13, 2011 at 4:34 am | Permalink

    Nice write-up Chris,

    once made a similar thing but not with WordPress at that time. Nice to know how to pull this off with WP based sites.

    grtz
    ToM.

One Trackback

  1. By designfloat.com on March 16, 2011 at 7:52 pm

    Using Session Variables and Domain Aliases to Change the WordPress URL…

    Learn how to change the WordPress URL using session variables, constants, and domain aliases. Use this technique to enable unique functionality based on entrance URL and to also keep site-wide links rewriting themselves correctly….

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>