2

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/
Community
  • 1
  • 1
PostMans
  • 338
  • 5
  • 18
  • So what does your http servers error log file tell you about the cause of that internal error? – arkascha Jan 04 '17 at 08:56
  • And BTW it should be `^([a-z0-9-]+)\.domain\.com$`... – arkascha Jan 04 '17 at 08:56
  • Also I fail to see that you actually _use_ the host name you took care to capture in that condition. – arkascha Jan 04 '17 at 08:58
  • @arkascha : Thank you for your comment. I have updated my question. I must say I am not an expert on .htaccess :( – PostMans Jan 04 '17 at 09:05
  • 1
    Sure, the endless redirection loop is because in the _next_ roundtrip through the rewriting engine the same condition obviously matches too... You can prevent that using the `END` flag instead of the `L` flag on newer versions of apache, on older versions you need an additional condition. _And_, as said: you should _do_ something with that host name you captured... – arkascha Jan 04 '17 at 09:10
  • @arkascha : Could you explain what you mean with capture hostname and what to do ? – PostMans Jan 04 '17 at 10:04
  • 1
    The brackets in `([a-z0-9-]+)` _capture_ the host name, the string before that literal dot following it. You usually capture that because you want to use it later, otherwise, why capture it? Usually one hands over such thing as an additional GET argument or as environment variable. – arkascha Jan 04 '17 at 10:08
  • @arkascha : Thanks for your comment. Your input is appreciated. – PostMans Jan 04 '17 at 12:35

1 Answers1

3

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) {
        // ...    
    }
}
sepehr
  • 17,110
  • 7
  • 81
  • 119
  • Thanks, I tested a bit and your result is correct. However I see now thats a redirect to index.php/team/team1 instead that the URL stays on http://team1.domain.com. After further searching on Google, it looks like CodeIgniter can not handle this correctly ? – PostMans Jan 04 '17 at 12:36
  • It has nothing to do with CodeIgniter; Apache does all the rewriting job before the request hits CodeIgniter. And **no**, it does not redirect the request. There are no `R` flags there. However, you will need to map all those subdomains in a way that they resolve to your application. You probably gonna need a [wildcard CNAME record](https://guides.co/g/getting-started-with-ghost/8866). After that, you also need to [configure Apache](https://www.digitalocean.com/community/questions/how-how-do-i-use-wildcard-subdomains-with-wordpress-multisite?answer=13591) to know about the wildcard subdomains. – sepehr Jan 04 '17 at 13:26
  • I have the wildcard DNS, but I guess the hoster has to do something @ Apache part (as you suggested). I will contact them. Thanks for the reply – PostMans Jan 04 '17 at 13:45
  • Does `team1.domain.com` hit your CodeIgniter application? – sepehr Jan 04 '17 at 13:53
  • Yes now it does, however team1.domain.com goes now to domain.com/team/team1 (as the route in routes.php). I think that the hoster needs something to change in Apache (as you said) – PostMans Jan 04 '17 at 13:58
  • If the subdomain(s) hit your application and the request gets rewritten, there's nothing they can do for you. Must be something else... [What's your Apache version?](https://stackoverflow.com/questions/166607) Might be the [`END` flag](https://httpd.apache.org/docs/current/rewrite/flags.html#flag_end) as it's 2.4-only. If you have 2.2.x version of Apache, we need to make a few changes to the rules. – sepehr Jan 04 '17 at 14:23
  • We get an echo team1 back. I checked DirectAdmin and we are running Apache 2.4.23. – PostMans Jan 04 '17 at 14:52
  • I just tested the rules locally; they're working well without triggering a redirection. No idea why you get redirected. – sepehr Jan 04 '17 at 15:00
  • Thank you for your help. For now the from subdomain to /team/team1 works (we don't have a wildcard certificate yet). – PostMans Jan 05 '17 at 08:17