通配符子域和CodeIgniter路由

时间:2017-01-04 08:49:43

标签: php .htaccess codeigniter mod-rewrite url-rewriting

我目前正在使用CodeIgniter 3.我想创建动态子域名,例如team1.domain.comteam2.domain.com等。

这些域需要指向控制器Team,并且指向该控制器中的show_Team方法。

我已经在StackOverflow上阅读了几个QA,但它们似乎都不适用于我。

目前,我有:

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]

作为路线:

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

但是这给了我一个 500内部错误

StackOverflow上发布的几个略有不同的选项也无效。

更新

错误日志告诉我:

[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/

当我将其更新为(如评论中所示):

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

它给了我这个错误:

[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/

1 个答案:

答案 0 :(得分:3)

试试这个;我通过评论解释:

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

在这里,我想你想将SUBDOMAIN作为某种团队标识符传递给控制器​​方法。

然后,你的路线应该是这样的:

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

与只与一个细分受众群匹配的(:any)相反,(.+)可以与多个细分受众群匹配。 $1是对(.+)所捕获内容的反对。

team/之后的任何内容都将作为参数传递给您的控制器方法:

class Team extends CI_Controller {

    // ...

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