php - 通配符子域和 CodeIgniter 路由

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

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

这些域需要指向 Controller Team,并专门指向该 Controller 中的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/

最佳答案

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

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 作为某种团队标识符传递给 Controller ​​方法。

那么,你的路线应该是这样的:

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

与仅匹配一个段的 (:any) 不同,(.+) 可以匹配多个段。 $1 是对 (.+) 捕获内容的反向引用。

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

class Team extends CI_Controller {

    // ...

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

关于php - 通配符子域和 CodeIgniter 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41459620/

相关文章:

PHP:裁剪文件/目录路径

PHP页面获取MySQL查询结果

php - 在保留测试数据的同时在 laravel 5.2 迁移中添加字段的过程是什么?

javascript - 如果没有则添加查询字符串

PHP - Codeigniter - 连接到 PostgreSQL 失败

javascript - 使用 Codeigniter 发布 JSON 返回 500 错误

php - Symfony 2 Doctrine 计数

apache - 需要在.htaccess中为Magento网站创建RewriteRule

.htaccess - curl:提供apache .htaccess文件的用户名和密码

php - 从URL Codeigniter获得值(value)