Welcome Guest, Not a member yet? Register   Sign In
Wildcard subdomain points to controllers
#1

[eluser]Tom Schlick[/eluser]
ok im sure someone on here has to have written a custom routes function to do this. basically i want something like

Code:
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
Code:
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
#2

[eluser]Sarfaraz Momin[/eluser]
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.

Code:
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 $1 !^(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 $1 !^(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
#3

[eluser]Tom Schlick[/eluser]
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

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($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;
        }
        
        // Can't find the requested controller...
        show_404($segments[0]);
    }
}
?>

EDIT** made sure to check that the subdomain wasnt 'www'
#4

[eluser]Tom Schlick[/eluser]
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
#5

[eluser]Tom Schlick[/eluser]
anybody have any idea of how to do this?
#6

[eluser]chrisnharvey[/eluser]
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:
Code:
{
     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:
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))
                    {
                         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($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;
        }
        
        // Can't find the requested controller...
        show_404($segments[0]);
    }
}
?>

Hope this helps Smile

- Chris




Theme © iAndrew 2016 - Forum software by © MyBB