Codeigniter将多个域路由到相同的控制器和功能,但参数不同

问题描述 投票:0回答:2

是否可以将多个域路由到单个控制器/功能,但参数不同?

例如:

some_domain.com  -> sites/display/site_slug_1
other_domain.com -> sites/display/site_slug_2

“站点”是控制器,“显示”是功能。

是否可以仅将新域添加到routes.php文件并将其重定向到适当的uri?


无法回答我自己的问题,所以我在这里发布解决方案:

我最终将这样的内容添加到routes.php文件中

//define each domain and it's route
$sites_routes = array();
$sites_routes['domain1.com'] = 'sites/display/site_slug_1';
$sites_routes['domain2.com'] = 'sites/display/site_slug_2';

//get domain name
$host = $_SERVER['HTTP_HOST'];
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
//define each domain and it's route
$sites_routes = array();
$sites_routes['domain1.com'] = 'sites/display/site_slug_1';
$sites_routes['domain2.com'] = 'sites/display/site_slug_2';

//get domain name
$host = $_SERVER['HTTP_HOST'];
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);

//build the routes
if(isset($sites_routes[$matches[0]]))
{    
    $route['default_controller']       = $sites_routes[$matches[0]];    
    $route['(:any)']                   = $sites_routes[$matches[0]].'/$1';       
}
else
{
    $route['default_controller']       = 'home';    
}    
dns routing routes
2个回答
0
投票

您可以在不同的域上使用.htaccess进行映射


0
投票
//get domain name
    $host = $_SERVER['HTTP_HOST'];
    preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
    //define each domain and it's route
    $sites_routes = array();
    $sites_routes['meilibosi.com'] = 'mlbs';
    $sites_routes['qunar.ir'] = 'longyueco';

    //get domain name
    $host = $_SERVER['HTTP_HOST'];
    preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);

    //build the routes
    if(isset($sites_routes[$matches[0]]))
    {
        $route['default_controller']       = $sites_routes[$matches[0]];
        $route['(:any)']                   = $sites_routes[$matches[0]]."/$1";
    }
    else
    {
        $route['default_controller']       = "mlbs";
    }
© www.soinside.com 2019 - 2024. All rights reserved.