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.

图片转代码服务由CSDN问答提供 功能建议

花了一天的时间来试图解决这个问题,最后需要寻求帮助。< / p>

我有一堆文件夹,我想进入子域名。 我已按照下面的教程在第1步中在DNS中设置了通配符重定向,并在第2步中编辑了我的virualhost。 这似乎已经计划好了。

但是我不确定步骤3背后的逻辑。下面的代码如何允许我显示子域中文件夹的内容? 我无法弄清楚我应该尝试和编码的逻辑 - 我认为我显然遗漏了一些明显的东西。

  $ serverhost = explode('。',$ _ SERVER [“HTTP_HOST”]); 
 $ sub = $ serverhost [0]; 
if($ sub =“  www“){
 $ sub =”“; 
} 
   
 
 

(教程中的文字)

好的,这是正在发生的事情。 您在主php 文件中插入此代码,它的作用是检查 域的子域部分(即:thishere.yourdomain.com)是否为www。 如果是这样,它只是空的 $ sub,否则,$ sub将包含你的subdomain关键字。 现在,您可以检查是否($ sub&gt;“”)并在 a子域存在时对您的代码采取适当的操作,以显示基于该值的页面。

(教程链接) http://www.wiredstudios.com/php-programming/setting-up-wildcard-dns-for-subdomains-on-cpanel.html

提前致谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题