0

Spent the better part of a day trying to get my head around this and finally need to ask for some help.

I have a bunch of folders which i want to make into subdomains. I have followed the tutorial below and have set up a wildcard redirect in my DNS in step 1 and edited my virualhost in step2. This seems to have gone to plan.

However i am unsure of the logic behind step 3. How does the code below allow me to display content from a folder in a subdomain? i cant figure out what logic i am supposed to try and code - i think i am clearly missing something obvious here.

$serverhost = explode('.',$_SERVER["HTTP_HOST"]);
$sub = $serverhost[0];
if ($sub = "www") {
$sub = "";
}

(text from tutorial)

OK, here's what's taking place. You insert this code in your main php file and what it does is check to see if the subdomain portion of the domain (ie: thishere.yourdomain.com) is www. If so, it just nulls $sub, otherwise, $sub will contain your subdomain keyword. Now, you can check if ($sub > "") and take appropriate action with your code if a subdomain exists, to display a page based on that value.

(tutorial link) http://www.wiredstudios.com/php-programming/setting-up-wildcard-dns-for-subdomains-on-cpanel.html

Thanks in advance.

3
  • Personally I'd tell you to disregard that tutorial, if you are trying to turn a known number of folders into sub-domains then they should be configured as separate virtual hosts. That tutorial is for if you want to turn a sub-domain into a variable to use in your scripts (such as one sub-domain per user profile on your site). Commented Jul 3, 2014 at 16:14
  • @AeroX The folders will not be of a known number once i figure it out. They are known only for testing. Commented Jul 3, 2014 at 16:16
  • 1
    In that tutorial you wouldn't even need separate folders, the variable $sub would contain whatever the user provided as the sub-domain and you can use that in your script to generate whatever content you like based upon it. Commented Jul 3, 2014 at 16:18

1 Answer 1

2

mmhh well, in fact, this code only permit you to get what subdomain is called.

So if you want to display the content of the folder corresponding to your subdomain, you have to scan your directory, then check if the folder called by subdomain exists, and then include script from this folder.

A simple way to do it is :

$scan = scandir('.'); // scan the current directory

if( in_array($sub, $scan) && is_dir($sub) ){

    require( $sub.'/yourscript.php');
}

But this mean that your whole appication is designed in function of the $sub value, each include, each file prefixing etc ...

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.