Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to create dynamic subdomain in codeigniter?

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

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

For eg:

i Want www.username.mysite.com/login or www.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.

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

like image 481
Nilay Patel Avatar asked Mar 16 '12 11:03

Nilay Patel


1 Answers

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>

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;
}
like image 152
Gaurav Avatar answered Sep 18 '22 08:09

Gaurav