Is there an easy way to create subdomains on codeigniter?

ぃ、小莉子 提交于 2019-11-27 14:16:44

问题


Is there an easy way to create subdomains on codeigniter like api.site.com?


回答1:


The approach you take depends on how different the subdomain is from the main site. If they are very similar and will be using the same codebase:

$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/';

If you need some settings to be different on a different subdomain, you could create a config file like so:

switch($_SERVER['HTTP_HOST']){
   case 'www.example.com':
       // settings specific to www subdomain
       $config['foo'] = 'bar';
   break;

   case 'apl.example.com':
       // settings specific to apl subdomain
       $config['foo'] = 'baz';
   break;
}

Another approach would include setting up a separate application folder for the subdomain, but pointing at the same system folder. If you organize your filesystem like this:

example.com
  common
    system
    application
  www
    htdocs
    application
  apl
    htdocs
    application

You could then point the index.php file in each htdocs folder at the common/system directory. You could also put code you want to share between all subdomains in common/application and include them from your code.




回答2:


You need to create the subdomain in your DNS or else setup a wildcard for any subdomain to work first.

Once you've got that running I guess it's a case of switching the base_url in your config file.

if ($_SERVER['SERVER_NAME'] == "api.blah.com") {
    $config['base_url'] = "http://api.blah.com/";
}
else {
    $config['base_url'] = "http://www.blah.com/";   
}


来源:https://stackoverflow.com/questions/3873554/is-there-an-easy-way-to-create-subdomains-on-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!