PostMans
PostMans

Reputation: 338

Wildcard subdomain and CodeIgniter routes

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.

I have read several QAs on StackOverflow, but none of them seem to work for me.

Currently, I have:

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

RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).domain.com [NC]
RewriteRule (.*) /index.php/team/$1/ [L,QSA]

And as route:

$route['team/(:any)'] = "Team/show_Team";

But this gives me a 500 internal error.

Several slightly different options posted on StackOverflow are also not working.

Update

The error log gives me:

[Wed Jan 04 09:52:15.013871 2017] [core:error] [pid 4792:tid 1332] (OS 123)The filename, directory name, or volume label syntax is incorrect.  : [client 127.0.0.1:61066] AH00132: file permissions deny server access: proxy:http://team1.domain.com/Team/show_Team/, referer: team1.domain.com/Team/show_Team/

When I updated it to (as given in the comments):

RewriteCond %{HTTP_HOST} ^([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule (.*) /index.php/team/$1/ [L,QSA]

It gives me this error:

[Wed Jan 04 10:01:35.959839 2017] [core:error] [pid 4792:tid 1320] [client 127.0.0.1:61351] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://team1.domain.com/

Upvotes: 2

Views: 2790

Answers (1)

sepehr
sepehr

Reputation: 18505

Try this; I explain through comments:

Options +FollowSymLinks

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

    # If it's not a file being accessed
    RewriteCond %{REQUEST_FILENAME} !-f
    # If it's not a directory being accessed
    RewriteCond %{REQUEST_FILENAME} !-d
    # And if it's domain.com, with or without www (no subdomain)
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
    # Rewrite all requests to index.php adding the query
    # string (QSA) and terminating all subsequent rewrite 
    # processings.
    # See: https://httpd.apache.org/docs/current/rewrite/flags.html#flag_end 
    RewriteRule ^(.*)$ /index.php/$1 [END,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 ^(.*)$ /index.php/team/%1/$1 [END,QSA]
</IfModule>

## Results:
# domain.com/foo/bar       => /index.php/foo/bar
# www.domain.com/foo/bar   => /index.php/foo/bar
# team1.domain.com/foo/bar => /index.php/team/team1/foo/bar
# team2.domain.com/foo/bar => /index.php/team/team2/foo/bar

Here, I supposed you want to pass the SUBDOMAIN as some kinda team identifier to the controller method.

Then, your route should be something like this:

$route['team/(.+)'] = "Team/show_Team/$1";

Contrary to (:any) that only matches one segment, (.+) can match against multiple segments. $1 is a back-refrence to what is captured with (.+).

Anything after team/ will be passed to your controller method as arguments:

class Team extends CI_Controller {

    // ...

    public function show_Team($team_id, $foo, $bar) {
        // ...    
    }
}

Upvotes: 3

Related Questions