dongqi7631 2014-07-03 16:08
浏览 40
已采纳

带有.htaccess和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.

  • 写回答

1条回答 默认 最新

  • doudi7570 2014-07-03 16:19
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站