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!!






5 Comments
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/
Absolutely! To keep the code simple I would use a 24 hour format to accomplish this. It would look like something like this:
/**
* This is the current hour on your server.
* Make sure this is correct!!
*/
$theCurrentHour = date('G');
/**
* Now we test to see what we should
* set the session variable to.
*/
if ( ($theCurrentHour >= 8) && ($theCurrentHour < 12) ) {
$_SESSION('myURL') = 'http://mysite.com/111';
}
elseif ( ($theCurrentHour >= 12) && ($theCurrentHour < 16) ) {
$_SESSION('myURL') = 'http://mysite.com/222';
}
/**
* This is a catch-all
*/
else {
$_SESSION('myURL') = 'http://mysite.com/imnowhereman';
}
Thank you, Chris! You rock. I am definitely going to use that…. :)
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.
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
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….