【问题标题】:Can htaccess override CodeIgniter's routes.phphtaccess 可以覆盖 CodeIgniter 的 routes.php
【发布时间】:2019-08-23 07:45:02
【问题描述】:

我想知道是否可以在 Codeigniter 3 中使用 htaccess 覆盖 routes.php 规则。

例如,为了将动态子域指向同一个控制器并将子域作为参数传递,routes.php 无法做到这一点,而在 htaccess 中则非常简单。

另一个例子是用 URL 段屏蔽查询字符串。 Routes.php 不允许使用查询字符串,但 htaccess 再一次非常适合。

那么,作为一个普遍的问题,是否可以在 CodeIgniter 中对所有路由使用 htaccess 而不是使用 routes.php?

【问题讨论】:

    标签: .htaccess codeigniter routing url-routing url-masking


    【解决方案1】:

    我认为您可以在 codeigniter 中使用 htaccess 进行静态路由。但是对于像http://localhost/myproject/user/1这样的动态路由应用基础 你必须使用 routes.php。

    【讨论】:

      【解决方案2】:

      Codeigniter 路由配置用于路由模块/控制器/方法/变量模式。

      我认为,域/子域从此配置中消失,但是您可以使用基于 $_SERVER 变量的动态 base_url,然后从特定控制器获取字符串(子域)。

      根据我的配置,在 CI 2.x 上

      $config['base_url'] = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
      $config['base_url'] .= '://'. $_SERVER['HTTP_HOST'];
      $config['base_url'] .= isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != '80' && $_SERVER['SERVER_PORT'] != '443' ? ( ':'.$_SERVER['SERVER_PORT'] ) : '';
      $config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
      

      然后做这样的事情......

      $server = $_SERVER['HTTP_HOST'];
      $domain = preg_replace('#^www\.(.+\.)#i', '$1', $server);
      $domain = $this->extract_domain($domain);
      $subdomain = $this->extract_subdomains($server);
      
      function extract_domain($domain)
      {
          if(preg_match("/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i", $domain, $matches))
          {
              return $matches['domain'];
          } else {
              return $domain;
          }
      }
      
      function extract_subdomains($domain)
      {
          $subdomains = $domain;
          $domain = $this->extract_domain($subdomains);
      
          $subdomains = rtrim(strstr($subdomains, $domain, true), '.');
      
          return $subdomains;
      }
      

      【讨论】:

        猜你喜欢
        • 2019-07-30
        • 2017-08-02
        • 2013-01-25
        • 2012-07-09
        • 2020-09-09
        • 2015-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多