想在 codeigniter 中创建动态子域? [英] Want to create dynamic subdomain in codeigniter?

查看:38
本文介绍了想在 codeigniter 中创建动态子域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站中,我想为用户添加一个功能,以便在域中使用他们的用户名.

In my site i want to add an functionality for user to use their username with domain.

就像现在在 codeigniter 中一样,我想让用户使用他们自己的 url 登录网站并做其他事情.

Like in codeigniter right now i want to give the user to use their own url to login in site and do other stuff.

例如:

我想要 www.username.mysite.com/loginwww.username.mysite.com/category

所以在这里用户可以使用他们的凭据登录并添加类别.所以我的站点中有两个带有登录名和类别的控制器.

so here the user can login with their credential and add the category. so i have two controller in my site with login and category.

那么如何使用路由或 .htaccess 来做到这一点.

So how to do this with the routes Or .htaccess.

推荐答案

在服务器 Vhost 文件中使用此代码:

Use this code in server Vhost file:

<VirtualHost *:80>
    DocumentRoot "/home/yourdoma/public_html/yourdomain"
    ServerName yourdomain.com
    ErrorLog "logs/yourdomain.local-error.log"
    CustomLog "logs/yourdomain.local-access.log" common
    <Directory "/home/yourdoma/public_html/yourdomain">
        AllowOverride All
        Require all Granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/home/yourdoma/public_html/yourdomain"
    ServerName dummy-host.yourdomain.com
    ErrorLog "logs/yourdomain.com-error.log"
    CustomLog "logs/yourdomain.com-access.log" common
    ServerAlias *.yourdomain.com
    <Directory "/home/yourdoma/public_html/yourdomain">
        AllowOverride All
        Require all Granted
    </Directory>
</VirtualHost>

以及对于您的 codeigniter 配置文件:

and For your codeigniter config file:

$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
$schema = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'].'://' : 'http://';
$spl_host = explode("mysite.com", $host);
$subhost = '';
if (count($spl_host) == 2) {
    $subhost = current($spl_host);
}

if($host == $subhost.'mysite.com') {
    $config['base_url'] = $schema.$subhost.'mysite.com';
} else {
    $config['base_url'] = $schema.$host;
}

这篇关于想在 codeigniter 中创建动态子域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆