Dynamic Subdomain in codeigniter

CodeIgniter is a powerful PHP framework with a very small footprint, built for PHP coders who need a simple and elegant toolkit to create full-featured web applications. If you’re a developer who lives in the real world of shared hosting accounts and clients with deadlines, and if you’re tired of ponderously large and thoroughly undocumented frameworks

You can create a dynamic sub-domains
store_name.asvignesh.in
username.asvignesh.in

Step 1: DNS Configuration

First, we need to configure our DNS settings to make all subdomains resolve to a single address. If you are working on a live server, then you will have to change your DNS settings with the company who handles your DNS. This is most commonly your web host or domain registrar.

To cover each configuration in this tutorial would take too much time. Instead, ask your host for some help to set up wildcard subdomains. You might expect to add something like the line below to your DNS configuration.

A Record Like

*.yourdomain.com 111.111.1.11

Step 2: Web Server Configuration

<VirtualHost *:80>
DocumentRoot “/Users/densepixel/Sites/MAMP PRO/nettutsappfront”
ServerName yourdomain.com
ServerAlias yourdomain.com
<Directory “/Users/www”>
Options -Indexes
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot “/Users/www2″
ServerName yourdomain.com
ServerAlias *.yourdomain.com
<Directory “/Users/www2″>
Options -Indexes
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>

Step 3: Codeigniter Configuration

The easiest way to do this is by using the HTTP_HOST variable.
Open the config.php file, and find the variable $config[‘base_url’],
and replace it with the following code:

if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == “on”){$ssl_set = “s”;} else{$ssl_set = “”;}
$config['base_url'] = ‘http’.$ssl_set.’://’.$_SERVER['HTTP_HOST'];

Step 4: Default Controller Code

<?php
class Dashboard extends Controller {
function Dashboard()
{
parent::Controller();
$subdomain_arr = explode(‘.’, $_SERVER['HTTP_HOST'], 2); //creates the various parts
$subdomain_name = $subdomain_arr[0]; //assigns the first part ( sub- domain )
echo $subdomain_name; // Print the sub domain
}
}

Step 5: Extended Sub-domain Processing

<?php
class Dashboard extends Controller {
function Dashboard()
{
parent::Controller();
$subdomain_arr = explode(‘.’, $_SERVER['HTTP_HOST'], 2);
$subdomain_name = $subdomain_arr[0];
if($subdomain_name == “signup”)
redirect ( signup controller );
elseif($subdomain_name == “mail”)
redirect ( email controller );
else
 redirect ( someothercontroller );
}
}

One comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from

Subscribe now to keep reading and get access to the full archive.

Continue reading