Welcome Guest, Not a member yet? Register   Sign In
Wildcard subdomains and routing
#1

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
Reply
#2

You need to use RewriteRule instead of a redirect inside your htaccess file.
Reply
#3

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
Reply
#4

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?
Reply
#5

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.
Reply
#6

If you want to work with the HTTPS (SSL) protocol in all the calls to the subdomains, you must make the DNS Dynamic and change in the DNS Manager from your Domain Manager this:
add a register type A and in the name put * and in value put the ip from your site or if the site is the same where would be the domain then put @

in php you too get the subdomain and work with this without .htacces modifications, the difference is that in this php version dont change or hidde the subdomain in the address bar call
$array = explode('.',$_SERVER['SERVER_NAME']);
$parameter1=$array[0];
if($parameter1 != 'www')
{
//do this tasks for this parameter in the subdomain
// go to this page or controll
}
//run the controller from index.php (www.domain.com or domain.com)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB