Xhynk
Xhynk

Reputation: 13850

Differentiate between Subdomains and Subdirectories

I've got a site that is pretty customized, set up on subdomains; sitename.domain.com and it's got some pages (that are the same for ALL subdomains) sitename.domain.com/this-page. Every single site has "/this-page".

We've got someone interested in using some of the stuff we've developed, but they are MARRIED to using subdirectories; domain.com/sitename which would, of course, have domain.com/sitename/this-page as well.

My question is, I've got some code

$sN = 'http://www.' . $_SERVER['HTTP_HOST'];
$PAGE = $sN . '/this-page/';

is there a way I can differentiate between subdomains and subdirectories?

$setup = "GET THE HOME PAGE, REGARDLESS OF SETUP"
if($setup ( CONTAINS www.X.X.com)) { //do the code above }
else if ($setup ( CONTAINS www.X.com/X)) { //do different code }

Upvotes: 2

Views: 146

Answers (2)

NotGaeL
NotGaeL

Reputation: 8484

[EDIT] Tried the solution from http://www.php.net/manual/en/reserved.variables.server.php#100881 but didn't work for me, so I did this:

<?php
$sitename = 'sitename';
if (strpos($_SERVER['HTTP_HOST'],$sitename)!==false){
    echo 'you are in http://'.$_SERVER['HTTP_HOST'] . '/'; 
 // you are in http://sitename.domain.com/
} else {
    $path = explode('/',$_SERVER['PHP_SELF']);
    unset ($path[count($path)-1]);
    echo 'you are in http://' . $_SERVER['HTTP_HOST'] . implode('/',$path) .'/'; 
 // you are in http://www.domain.com/sitename/
}
?>

Upvotes: 2

Xhynk
Xhynk

Reputation: 13850

The answer from @elcodedocle is the better way of doing this. While they came up with that solution, I had ended up with a (yuckier) solution as well.

//Let's grab the current url for other uses
$cur = 'http://www.' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

//Is this subdomains or subdirectories?
$TYPE = explode('.', $cur);
if(isset($TYPE[3])){
    //Yay! It's subdomains! not stupid subdirectories! 
    $this_root = 'http://www.' . $_SERVER['HTTP_HOST'];

} else {
    //Boo.... It's stupid subdirectories....  ;o(
    $chunks = explode('/', $TYPE[2]);
    $this_root = 'http://www.' .$TYPE[1]. '.' .$chunks[0]. '/' .$chunks[1];
}
?>

This works as well, but it's a less graceful solution. I ended up using a slightly modified version of the code above, but for those of you wondering, this is how I got there before reloading SO. :)

Upvotes: 1

Related Questions