【问题标题】:Wildcard subdomain .htaccess and Codeigniter通配符子域 .htaccess 和 Codeigniter
【发布时间】:2011-06-05 20:51:55
【问题描述】:

我正在尝试创建正确的 .htaccess 以允许我这样映射:

http://domain.com/                --> http://domain.com/home 
http://domain.com/whatever        --> http://domain.com/home/whatever
http://user.domain.com/           --> http://domain.com/user 
http://user.domain.com/whatever   --> http://domain.com/user/whatever/

在这里,有人会输入上述网址,但在内部,它会像右侧的网址一样重定向。

子域也是动态的(也就是说,http://user.domain.com 不是实际的子域,而是 .htaccess 重写)

另外 /home 是我的默认控制器,因此没有子域会在内部强制它到 /home 控制器,并且它后面的任何路径(如上面的 #2 示例所示)将是该控制器内的(catch-all)函数。

同样,如果传递子域,它将作为(catch-all)控制器以及它的任何(catch-all)函数传递(如上面的 #4 示例所示)

希望我在这里没有问太多,但我似乎无法为此找出正确的 .htaccess 或路由规则(在 Codeigniter 中)。

httpd.conf 和主机都设置好了。

编辑#1

这是我的 .htaccess 即将接近但在某些时候搞砸了:

RewriteEngine On

RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

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

RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]

有了以上内容,当我访问:http://test.domain/abc/123 这是我在 $_SERVER var 中注意到的内容(我已经删除了一些字段):

Array
(
    [REDIRECT_STATUS] => 200
    [SERVER_NAME] => test.domain
    [REDIRECT_URL] => /abc/123
    [QUERY_STRING] => 
    [REQUEST_URI] => /abc/123
    [SCRIPT_NAME] => /index.php
    [PATH_INFO] => /test/abc/123
    [PATH_TRANSLATED] => redirect:\index.php\test\test\abc\123\abc\123
    [PHP_SELF] => /index.php/test/abc/123
)

您可以看到 PATH_TRANSLATED 没有正确形成,我认为这可能是把事情搞砸了?

【问题讨论】:

    标签: apache .htaccess codeigniter subdomain wildcard


    【解决方案1】:

    好的,我相信我已经解决了。这是我目前所拥有的。

    首先是.htaccess

    RewriteEngine On
    
    RewriteBase /
    
    # if REQUEST_URI contains the word "user" and the
    # SERVER_NAME doesn't contain a "." re-direct to the root
    # The reason this is done is because of how the last two rules
    # below are triggered
    RewriteCond %{REQUEST_URI} (user) [NC]
    RewriteCond %{SERVER_NAME} !\.
    RewriteRule (.*) / [L,R=301]
    
    # Allow files and directories to pass
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    
    # Codeigniter rule for stripping index.php
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [C]
    
    # Force wild-card subdomains to redirect.
    # E.g. http://me.domain/foo/bar/123 as http://domain/user/me/index.php/foo/bar/123/bar/123/
    RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).domain [NC]
    RewriteRule (.*) /index.php/user/%1/$1/ [L]
    

    最后是 routes.php

    <?php
    // Force routing to userhome controller if URL contains the word "user"
    // otherwise force everything else to home controller
    $route['user/:any'] = "userhome";
    $route[':any'] = "home";
    ?>
    

    正如您从上面看到的,一切正常。我唯一想不通的是为什么我使用子域时会重复最后一个参数?

    如果我这样做:http://domain/foo/bar/123

    然后我的 PATH_INFO 显示为 /foo/bar/123/ 这是完美的

    但如果我这样做:http://me.domain/foo/bar/123

    然后我的 PATH_INFO 显示为 /user/me/index.php/foo/bar/123/bar/123/ 大多数情况下哪个是可以的,但为什么最后参数会重复?

    所以是的,总的来说,我认为它有效。我唯一需要做的就是为我添加到我的 \controllers 的任何控制器设置几条路由。除非有办法解决?

    【讨论】:

      【解决方案2】:

      这应该可行。请测试并告诉我它是否有效:

      RewriteEngine On
      RewriteCond   %{HTTP_HOST}              ^[^.]+\.domain\.com$
      RewriteRule   ^(.+)                     %{HTTP_HOST}$1        [C]
      RewriteRule   ^([^.]+)\.domain\.com(.*) /$1$2                 [L]
      RewriteRule   ^(.*)                     /home$1               [L]
      

      【讨论】:

      • 感谢 takinbo,不幸的是这没有用。它导致服务器 500(由于可能的配置错误,请求超出了 10 个内部重定向的限制)
      • 我将对此进行测试并找出正确的组合。
      猜你喜欢
      • 2013-02-16
      • 2011-08-16
      • 1970-01-01
      • 2017-05-18
      • 2014-09-06
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多