douwei1904 2012-03-29 13:34
浏览 37

在PHP中重定向子域和域的标准情况

Guys!

I will explain what I want to do. If anyone can help with this, please, give some advices.

I have a website, let's call it http://mysite.com. And subdomain, let's say http://myname.mysite.com.

There is one configuration file inc.php with some personal data each for mysite.com and myname.mysite.com, example ICQ number. All general website files located in mysite.com directory.

I want to be able all general files of the mysite.com at the subdomain myname.mysite.com, in order not to create completely similiar files at subdomain. But with including individual configuration file, located at the myname.mysite.com, containing individual ICQ number.

This is the example how I want this to work:

  1. Main domain "mysite.com" has root folder "public_html" with pages: index.php, about.php, articles.php and configuration file inc.php with icq number "111-111". So when serfing "mysite.com" website, you see there ICQ number from inc.php.

  2. Subdomain "myname.mysite.com" has folder "public_html/subdomain/myname" with only a configuration file inc.php with ICQ number "222-222". So when going to myname.mysite.com you will see the web pages from "mysite.com" but the ICQ number will get from here "public_html/subdomain/myname/inc.php". And in address bar you will see web pages as: http://myname.mysite.com/index.php http://myname.mysite.com/about.php http://myname.mysite.com/articles.php

So visitors will think that they are serfing individual website myname.mysite.com, but in real, visitors will serf on the mysite.com webpages. So if I change content on mysite.com, this content will be changed on myname.mysite.com.

I need all this staff cause I want to organize affiliate programm with giving personal subdomain name to my partners but with personal configuration file inc.php, that will take ICQ number for consulting.

I guess I've tried to explain what I want to do. God, do hard to explain this situation. Ah, forgot to mention, all this I want to do on PHP.

  • 写回答

2条回答 默认 最新

  • dpii73380 2012-03-29 14:07
    关注

    If the only thing you want changing is the inc.php file, I would suggest structuring it slightly differently (the code below assumes only 1-level subdomains, such as subdomain.mysite.com, and some modifications in points (3) and (4) will be needed for deeper subdomains, such as sub.subdomain.mysite.com).

    1) In your apache virtual host configuration, define all subdomains to be aliases for the main domain:

    <VirtualHost *:80>
        ...
    
        SernarName mysite.com
        ServerAlias *.mysite.com
    
        ...
    </VirtualHost>
    

    2) Have all your PHP files for all subdomains in one directory and point your apache config to that dir.

    3) In that directory, create a bunch of inc files: inc.php, inc-subdomain1.php, inc-subdomain2.php and so on.

    4) Have some sort of config.php file, which you include from all other files in your site, and have it contain something like this:

    if(isset($_SERVER['HTTP_HOST'])) {
        $host = explode('.', strtolower($_SERVER['HTTP_HOST']));
        if(count($host) == 3) {   //have something more than just mysite.com
            $subdomain = $host[0];
            if($subdomain == 'www') {     //www.mysite.com is the same as just mysite.com
                $subdomain == '';
            }
        }
        else {      //if just mysite.com, then no subdomains
            $subdomain = '';
        }
    }
    else {  //if no http_host info exists, then assume it's just mysite.com
        $subdomain = '';
    }
    
    $incfile = 'inc' . ($subdomain == '' ? '' : "-$subdomain") . '.php';
    if(!file_exists($incfile)) {    //if no include file for the requested subdomain, just use mysite.com
        $incfile = 'inc.php';
    }
    
    require_once($incfile);
    

    Now, all of this, of course will give you a lot of flexibility, but may not be necessary if all you need is just the different ICQ numbers. In that case, I would change point (4) code to have only 1 include file, say inc.php, and in that file have something like this:

    $icq_default = '111-111';
    $icq_nums = array('subdomain1' => '222-222',
                      'subdomain2' => '333-333',
                      ...);
    
    if(isset($_SERVER['HTTP_HOST'])) {
        $host = explode('.', strtolower($_SERVER['HTTP_HOST']));
        if(count($host) == 3) {   //have something more than just mysite.com
            $subdomain = $host[0];
            if($subdomain == 'www') {     //www.mysite.com is the same as just mysite.com
                $subdomain == '';
            }
        }
        else {      //if just mysite.com, then no subdomains
            $subdomain = '';
        }
    }
    else {  //if no http_host info exists, then assume it's just mysite.com
        $subdomain = '';
    }
    
    $required_number = isset($icq_numbers[$subdomain]) ? $icq_numbers[$subdomain] : $icq_default;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?