简体   繁体   中英

Wildcard Subdomain with .htaccess and php

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM