EllisLab text mark
Advanced Search
     
Wildcard subdomain points to controllers
Posted: 12 January 2009 02:32 AM
Avatar
Joined: 2008-01-17
426 posts

ok im sure someone on here has to have written a custom routes function to do this. basically i want something like

store.mysite.com/item/696

to go to

mysite
.com/store/item/696 

i dont want it just to redirect but to actually work


i already have the dns set up for wildcard subdomains so all i need is the MY_Routes.php code to make it work.

so does anyone have this already done? ive searched all over the forums but i cannot find it


thanks in advance! smile

*EDIT*

i also need it to work with subdirectories in the controllers folder so

store.mysite.com/cart/item/696

would go to

mysite
.com/store/cart/item/696 

i only need it to work on a one subdomain system so there will never be whatever.store.mysite.com

 Signature 

aka trs21219
CodeSanity | Github | LinkedIn | Facebook | Twitter | Last.fm

 
Posted: 12 January 2009 04:18 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-09-13
247 posts

I have done it but to some specific subdomain names with wildcard set. Well you can still do it with a little help using .htaccess file. I want the same functionality in my existing project but I am still not to the point where I would need it. I have an example .htaccess file which might help you.

Options Includes +ExecCGI
Options 
+FollowSymLinks
RewriteEngine On

## Rewrite domain.com/ to welcome.domain.com/
RewriteCond %{HTTP_HOST} ^(site.com)$ [NC]
RewriteRule 
^$ http://welcome.%1/ [R=301,L]

## Rewrite www.domain.com/ to welcome.domain.com/
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule 
^$ http://welcome.%1/ [R=301,L]

## Rewrite domain.com/... to welcome.domain.com/...
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^((site)\.(com))$ [NC]
RewriteCond 
$!^(index\.php|runtime|robots\.txt|assets)
RewriteRule ^(.*)$ http://welcome.%{HTTP_HOST}/index.php/welcome%{REQUEST_URI} [QSA,L]

## Rewrite www.domain.com/... to welcome.domain.com/...
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule 
^(.*)$ http://welcome.%1/index.php/welcome%{REQUEST_URI} [QSA,L]

## Rewrite something.domain.com/... to something.domain.com/...
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^((.+)\.)?((site)\.(com))$ [NC]
RewriteCond 
$!^(index\.php|runtime|robots\.txt|assets)
RewriteRule ^(.*)$ http://%{HTTP_HOST}/index.php/%2%{REQUEST_URI} [QSA,L]

## Fix trailing slash
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule 
^(.+[^/])$ $1[L] 

Have a good day !!!

-Sarfaraz

 Signature 

Sarfaraz Momin.
PHP With Us

 
Posted: 12 January 2009 04:44 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-17
426 posts

nice i got it to work without htaccess! smile im not sure if there are going to be any implications down the road with the uri structure since the subdomain name wont be in there but we will see….

here is MY_Routes.php

notice the subdomain code above all of the normal code

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Router Class
*
* Extends CI Router
*
* @author     Original by EllisLab - extension by CleverIgniters
* @see        http://codeigniter.com
*/

class MY_Router extends CI_Router {
    
    
/**
     * Constructor
     *
     * @access    public
     */
    
function MY_Router()
    
{
        parent
::CI_Router();
    
}
    
    
// --------------------------------------------------------------------
    
    /**
     * Validate Routing Request
     *
     * @access    public
     */
    
function _validate_request($segments)
    
{
        
        
// CHECK TO SEE IF SUBDOMAIN AND GO ON FROM THERE
        
$http explode('.'$_SERVER['HTTP_HOST']);
        if(
count($http) > 2)
        
{
            
if($http[0] != 'www')
            
{
                
if(is_dir(APPPATH.'controllers/'.$http[0]))
                
{
                    
// Set the directory and remove it from the segment array
                    
$this->set_directory($http[0]);
                    
//$segments = array_slice($segments, 1);
                    
                    
if (count($segments) > 0)
                    
{
                        
// Does the requested controller exist in the sub-folder?
                        
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                        
{
                            show_404
($this->fetch_directory().$segments[0]);
                        
}
                    }
                    
else
                    
{
                        $this
->set_class($this->default_controller);
                        
$this->set_method('index');
                    
                        
// Does the default controller exist in the sub-folder?
                        
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                        
{
                            $this
->directory '';
                            return array();
                        
}
                    
                    }
        
                    
return $segments;
                
}
                
else
                
{
                    
if (file_exists(APPPATH.'controllers/'.$http[0].EXT))
                    
{
                        
return $segments;
                    
}
                }
            }
        }
        
//END SUBDOMAIN LOGIC HERE
        
        // Does the requested controller exist in the root folder?
        
if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        
{
            
return $segments;
        
}
        

        
// Is the controller in a sub-folder?
        
if (is_dir(APPPATH.'controllers/'.$segments[0]))
        
{        
            
// Set the directory and remove it from the segment array
            
$this->set_directory($segments[0]);
            
$segments array_slice($segments1);
            
            if (
count($segments) > 0)
            
{
                
// Does the requested controller exist in the sub-folder?
                
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                
{
                    show_404
($this->fetch_directory().$segments[0]);
                
}
            }
            
else
            
{
                $this
->set_class($this->default_controller);
                
$this->set_method('index');
            
                
// Does the default controller exist in the sub-folder?
                
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                
{
                    $this
->directory '';
                    return array();
                
}
            
            }

            
return $segments;
        
}
        
        
// Can't find the requested controller...
        
show_404($segments[0]);
    
}
}
?> 

EDIT** made sure to check that the subdomain wasnt ‘www’

 Signature 

aka trs21219
CodeSanity | Github | LinkedIn | Facebook | Twitter | Last.fm

 
Posted: 12 January 2009 05:03 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-17
426 posts

ya so i got that all working now but i need a MY_URI.php to add the subdomain into the uri segments. it would have to be index 0 since it will be the controller. if anyone knows of any please let me know smile

 Signature 

aka trs21219
CodeSanity | Github | LinkedIn | Facebook | Twitter | Last.fm

 
Posted: 15 January 2009 06:26 PM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-17
426 posts

anybody have any idea of how to do this?

 Signature 

aka trs21219
CodeSanity | Github | LinkedIn | Facebook | Twitter | Last.fm

 
Posted: 17 March 2011 02:24 PM   [ # 5 ]   [ Rating: 0 ]
Joined: 2010-10-14
8 posts

Hello,

Sorry for reopening such an old topic, but I stumbled upon this post when searching Google for the same result and thought I would post my complete solution that was never fully resolved in this post. So just in case anyone may search for it in the future, I will post the solution.

To add the subdomain segment to the beginning of the segments array, simply use “array_shift” and “array_unshift” before returning the segments.

Array_shift is used to remove the default controller from the beginning of the segments array if you are accessing the root of the subdomain (e.g. user.example.com).

Array_unshift is used to add the subdomain onto the beginning of the segments array.

Like this:

{
     
if (file_exists(APPPATH.'controllers/'.$http[0].EXT))
     
{
          
if($segments[0] == $this->default_controller// Check if default controller and remove 
          
{
               array_shift
($segments);
          
}
          array_unshift
($segments$http[0]);
          return 
$segments;
     
}

So the full MY_Router subclass will now be:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Router Class
*
* Extends CI Router
*
* @author     Original by EllisLab - extension by CleverIgniters
* @see        http://codeigniter.com
*/

class MY_Router extends CI_Router {
    
    
/**
     * Constructor
     *
     * @access    public
     */
    
function MY_Router()
    
{
        parent
::CI_Router();
    
}
    
    
// --------------------------------------------------------------------
    
    /**
     * Validate Routing Request
     *
     * @access    public
     */
    
function _validate_request($segments)
    
{
        
        
// CHECK TO SEE IF SUBDOMAIN AND GO ON FROM THERE
        
$http explode('.'$_SERVER['HTTP_HOST']);
        if(
count($http) > 2)
        
{
            
if($http[0] != 'www')
            
{
                
if(is_dir(APPPATH.'controllers/'.$http[0]))
                
{
                    
// Set the directory and remove it from the segment array
                    
$this->set_directory($http[0]);
                    
//$segments = array_slice($segments, 1);
                    
                    
if (count($segments) > 0)
                    
{
                        
// Does the requested controller exist in the sub-folder?
                        
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                        
{
                            show_404
($this->fetch_directory().$segments[0]);
                        
}
                    }
                    
else
                    
{
                        $this
->set_class($this->default_controller);
                        
$this->set_method('index');
                    
                        
// Does the default controller exist in the sub-folder?
                        
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                        
{
                            $this
->directory '';
                            return array();
                        
}
                    
                    }
        
                    
return $segments;
                
}
                
else
                
{
                    
if (file_exists(APPPATH.'controllers/'.$http[0].EXT))
                    
{
                         
if($segments[0] == $this->default_controller)
                         
{
                              array_shift
($segments);
                         
}
                         array_unshift
($segments$http[0]);
                         return 
$segments;
                    
}
                }
            }
        }
        
//END SUBDOMAIN LOGIC HERE
        
        // Does the requested controller exist in the root folder?
        
if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        
{
            
return $segments;
        
}
        

        
// Is the controller in a sub-folder?
        
if (is_dir(APPPATH.'controllers/'.$segments[0]))
        
{        
            
// Set the directory and remove it from the segment array
            
$this->set_directory($segments[0]);
            
$segments array_slice($segments1);
            
            if (
count($segments) > 0)
            
{
                
// Does the requested controller exist in the sub-folder?
                
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                
{
                    show_404
($this->fetch_directory().$segments[0]);
                
}
            }
            
else
            
{
                $this
->set_class($this->default_controller);
                
$this->set_method('index');
            
                
// Does the default controller exist in the sub-folder?
                
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                
{
                    $this
->directory '';
                    return array();
                
}
            
            }

            
return $segments;
        
}
        
        
// Can't find the requested controller...
        
show_404($segments[0]);
    
}
}
?> 

Hope this helps smile

- Chris