【问题标题】:Wildcard subdomains for a single codeigniter application单个 codeigniter 应用程序的通配符子域
【发布时间】:2011-08-19 23:23:58
【问题描述】:

基本上我正在尝试使用本地化子域制作一个 codeigniter 应用程序,这样如果我访问:

ca.site.com -> 它将运行相同的系统,但显示加拿大内容。 us.site.com -> 仍然运行相同的系统,但显示美国内容。

另外,如果我访问 www.site.com,它会自动使用 ip2country 获得正确的本地化,并且会像从英国重定向到 uk.site.com 一样被重定向

只有 1 个系统文件夹和 1 个应用程序文件夹:

根 /系统 /应用 index.php

现在我希望 URL 保留在它们所在的本地化中。喜欢:

uk.site.com/profile/1 将访问配置文件控制器 ca.site.com/profile/3 也将访问 uk.site.com 使用的同一控制器

我将如何实现这一点?

如果我的询问有点粗略,请原谅我。但我希望有人能帮助我。

【问题讨论】:

    标签: php codeigniter wildcard subdomain


    【解决方案1】:

    您必须找到一种方法来允许动态子域重定向(显然可以使用一点 .htaccess mod_rewrite 魔法),然后制作一个主控制器,索引方法的第一个参数是语言,然后拥有子域重定向从 (.*).site.com/(.*) = site.com/$1/$2 开始,这应该可以,但它不遵循 CodeIgniter 的传统 MVC 方法,它更像是一种 hack,而不是正确的方法,我相信其他人会知道更多语义上的工作方式。

    如果我没记错的话,子域重定向记录在 apache.org 上的某个地方,你会发现比我在 google 上提供的信息更多

    【讨论】:

      【解决方案2】:

      可能有很多方法可以做到这一点......但我能想到的一种方法是在 index.php 文件中放置一些东西,在该文件中建立了预加载的自定义配置值,可以做一些事情像这样……

      /*
       * -------------------------------------------------------------------
       *  CUSTOM CONFIG VALUES
       * -------------------------------------------------------------------
       *
       * The $assign_to_config array below will be passed dynamically to the
       * config class when initialized. This allows you to set custom config
       * items or override any default config values found in the config.php file.
       * This can be handy as it permits you to share one application between
       * multiple front controller files, with each file containing different
       * config values.
       *
       * Un-comment the $assign_to_config array below to use this feature
       *
       */
          $region_prefix_var = explode('.', $_SERVER['HTTP_HOST']);
          $assign_to_config['region_prefix'] = $region_prefix_var[0];
      

      然后在你的 config.php 文件中

      global $region_prefix_var;
      $config['base_url'] = $region_prefix_var[0].".site.com/";
      

      ...或者您可以根据 $_SERVER['HTTP_HOST'] 值覆盖 $assign_to_config 数组中 index.php 文件中的 base_url。然后在任何其他控制器或任何你可以基于的东西中

      $this->config->item('region_prefix');
      

      然后只需将所有子域指向同一目录并在您的应用程序中进行本地化。

      【讨论】:

        猜你喜欢
        • 2023-04-09
        • 1970-01-01
        • 2011-06-05
        • 2017-05-18
        • 2013-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多