dpoppu4300 2017-01-04 08:49
浏览 82
已采纳

通配符子域和CodeIgniter路由

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/
  • 写回答

1条回答 默认 最新

  • doudun3040 2017-01-04 10:24
    关注

    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) {
            // ...    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的