CodeIgniter Forums

Full Version: Wildcard subdomains and routing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Howdy,

I'm currently using CodeIgniter 3. I want to create dynamic subdomains like team1.domain.com, team2.domain.com, etc.
These domains need to point to the controller Team and a specifically to the show_Team method in the that controller.

Routing is as follow
PHP Code:
$route['team/(:any)'] = "Team/show_Team"

I had severall rules in the .htaccess, but these do a redirect to /team/team1, but I want to work from the subdomain.
Like as said team1.domain.com

Anyone an idea
You need to use RewriteRule instead of a redirect inside your htaccess file.
Thanks Diederik for your answer

I have the following

Code:
# If it's not starting with www
    RewriteCond %{HTTP_HOST} !^www
    # And is a subdomain
    RewriteCond %{HTTP_HOST} ^([a-z0-9-]+)\.domain\.com$ [NC]
    # Rewrite the request to index.php/test/SUBDOMAIN/whatever...
    RewriteRule ^(.*)$ /index.php/team/%1/$1 [END,QSA]
(and some variations)

However its using a RewriteRule
I'm not quite a htaccess guru but the rewrite rule looks ok, atleast your not using a [R] flag for a redirect. Are you shure the redirect is cause by the htaccess file?
I am not sure if its the .htaccess or something else

The complete .htaccess is

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Force HTTPS and no WWW
    # RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    # RewriteRule .* https://%1%{REQUEST_URI} [L,R=301,NE]

    # RewriteCond %{HTTPS} off
    # RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ ./index.php [L,QSA]

    # If it's not starting with www
    RewriteCond %{HTTP_HOST} !^www
    # And is a subdomain
    RewriteCond %{HTTP_HOST} ^([a-z0-9-]+)\.domain\.com$ [NC]
    # Rewrite the request to index.php/test/SUBDOMAIN/whatever...
    RewriteRule ^(.*)$ https://domain.com/team/%1 [END,QSA]

</IfModule>

As you can see the last RewriteRule is changed. For now when I go to https://team1.domain.com, the address bar shows https://domain.com/team/team1.

The route
PHP Code:
$route['team/(:any)'] = "Team/show_Team/$1"
is triggered and show_Team is called.
The problem is that the URL changes.