【问题标题】:How to set proper codeigniter base url?如何设置正确的codeigniter base url?
【发布时间】:2012-08-01 07:09:35
【问题描述】:

当我的网站在开发环境中时 - 它是 url: testurl.com

现在在生产服务器上,我的 codeigniter 应用的地址必须是 someurl.com/mysite/

我把它移到那里,每次我试图运行一些功能,例如 /home/test - 它让我进入 someurl.com/home/test - 这是错误的。

必须是 someurl.com/mysite/home/test - 如何解决?我确实设置了

$config['base_url'] = someurl.com/mysite/

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    如果您将其留空,则自版本2.0.0 起,框架将尝试autodetect

    但不在3.0.0,请看这里:config.php

    【讨论】:

      【解决方案2】:

      尝试将控制器添加到您的索引页面。只是在我自己的一个网站上查看了类似的结构,我将我的基本 url 设置为 = "somedomain.com/v2/controller"

      【讨论】:

      • 你能解释得更深入一点吗?
      • 据我了解,您的应用程序和系统文件夹位于一个名为“mysite”的文件夹中,并且据说我在自己的一个站点上有一个类似的结构。我将基本 url 设置为“somedomain.com/v2/controller”,其中 v2 是我的“mysite”文件夹版本,控制器(或控制器/函数)是您希望作为索引页面的控制器。
      【解决方案3】:

      你错过了双引号吗?

      $config['base_url'] = "someurl.com/mysite"

      【讨论】:

      • 我愿意,但改变没有帮助:)
      【解决方案4】:

      基本 URL 应该是绝对的,包括协议:

      $config['base_url'] = "http://somesite.com/somedir/";
      

      如果使用 URL 助手,那么base_url() 将输出上述字符串。

      将参数传递给base_url()site_url() 将导致以下结果(假设$config['index_page'] = "index.php";

      echo base_url('assets/stylesheet.css'); // http://somesite.com/somedir/assets/stylesheet.css
      echo site_url('mycontroller/mymethod'); // http://somesite.com/somedir/index.php/mycontroller/mymethod
      

      【讨论】:

      • +1 为您对绝对引用的回答...但是看着这个,我有一个疑问...拥有 CodeIgniter 文件和“somesite”文件不是一个坏主意相同的位置(即 www)?我的意思是这不会让 CodeIgniter 文件被外部机器人接受吗?只是好奇...
      • 可能,是的,有两件事需要考虑:1) 所有 CI 项目文件都应该以 if (!defined('BASEPATH')) exit('No direct script access allowed'); 作为第一行。这可以防止脚本自行运行,因为BASEPATH 常量没有以其他方式定义(无论是 CI 的前端控制器还是其他脚本)。 2) 只要​​您在 CI 的前端控制器 (index.php) 中指定它们的路径,您就可以安全地将 systemapplication 文件夹移到您的 webroot 之外(在您的情况下为 www)。基本上你的 webroot 将只包含 index.php 和资产。希望对您有所帮助。
      • 覆盖这个baseUrl的场景是什么
      【解决方案5】:

      在 config.php 中声明基本 url 后,(注意,如果仍然遇到同样的错误,请检查 autoload.php)

      $config['base_url'] = "http://somesite.com/somedir/";
      

      勾选“自动加载”

      CodeIgniter 文件结构:

      \application\config\autoload.php
      

      并在自动加载时启用:

      $autoload['helper'] = array(**'url',** 'file');
      

      而且它有效。

      【讨论】:

        【解决方案6】:

        检查

        配置>配置

        codeigniter 文件结构

        替换

        $config['base_url'] = "your Website url";
        

        $config['base_url']  =  "http://".$_SERVER['HTTP_HOST'];
        
        $config['base_url'] .= preg_replace('@/+$@', '', dirname($_SERVER['SCRIPT_NAME'])).'/';
        

        【讨论】:

        • https 检测缺失
        • ltrim($string, '/') 是一个更简单/更轻的选项时,为什么有人会费心使用preg_replace('@/+$@',...) 从字符串中删除前导斜杠?
        【解决方案7】:

        您可以为任何服务器使用默认的基本 URL 路径 必须在配置文件夹> config.php 文件中使用 替换你:

        $config['base_url'] = 'index.php';
        

        通过此代码: $config['base_url'] = "http://".$_SERVER['HTTP_HOST']; $config['base_url'] .= preg_replace('@/+$@','',dirname($_SERVER['SCRIPT_NAME'])).'/';

        它会从任何服务器自动找到您的文件夹

        【讨论】:

          【解决方案8】:

          这很有趣,这是快速且有效的代码:

          index.php

          /**
           * Define APP_URL Dynamically
           * Write this at the bottom of index.php
           *
           * Automatic base url
           */
          define('APP_URL', ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . "://{$_SERVER['SERVER_NAME']}".str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME'])); 
          

          config.php

          /*
          |--------------------------------------------------------------------------
          | Base Site URL
          |--------------------------------------------------------------------------
          |
          | URL to your CodeIgniter root. Typically this will be your base URL,
          | WITH a trailing slash:
          |
          |   http://example.com/
          |
          | If this is not set then CodeIgniter will guess the protocol, domain and
          | path to your installation.
          |
          */
          $config['base_url'] = APP_URL;
          

          CodeIgniter 摇滚!!! :)

          【讨论】:

            【解决方案9】:

            动态基础网址(codeigniter)

            只需用以下内容覆盖 config/config.php 中的行:

            $config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/';

            【讨论】:

            • 如果 spisstek.com 然后是 spisstek.com 。对于这两个网址,它都工作正常......我将在我的网络上使用它......链接在这里......
            【解决方案10】:
            $config['base_url'] = "http://".$_SERVER['SERVER_NAME']."/project_name/";
            

            这样你就可以配置你的 base_url ,然后就不用担心托管了。两者都适用于本地主机和服务器。

            【讨论】:

            • 如果您需要在 url 中添加 index.php,请在项目名称后添加它。要删除 index.php,您需要更改 .htaccess 并在 confiq 页面中进行更改
            【解决方案11】:
            Base url set in CodeIgniter for all url
            
            $config['base_url'] = "http://".$_SERVER['HTTP_HOST']."/";
            

            【讨论】:

              【解决方案12】:

              应用程序 > 配置 > config.php

              搜索 $config['base_url'] 并将您的网站设置为“//example.com”(跳过协议)

               $config['base_url'] = "//someurl.com/";
              

              这对我有用。

              【讨论】:

                【解决方案13】:

                这是我在hostinger.com中申请的服务器和实时站点,它工作正常

                第一个:$config['base_url'] = 'http://yoursitename.com'; (在 confing.php 中)

                2) : src="<?=base_url()?>assest/js/wow.min.js" (在查看文件中)

                3) : href="<?php echo base_url()?>index.php/Mycontroller/Method" (用于url链接或方法调用)

                【讨论】:

                  【解决方案14】:

                  像这样改变config.php。

                  $base_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
                  $base_url .= "://". @$_SERVER['HTTP_HOST'];
                  $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
                  $config['base_url'] = $base_url;
                  

                  【讨论】:

                    【解决方案15】:

                    基本 URL 应该是绝对的,包括协议:

                    $config['base_url'] = "http://".$_SERVER['SERVER_NAME']."/mysite/";
                    

                    此配置将在 localhost 和服务器中均有效。

                    不要忘记启用自动加载:

                    $autoload['helper'] = array('url','file');
                    

                    那么base_url()会输出如下

                    echo base_url('assets/style.css'); //http://yoursite.com/mysite/assets/style.css
                    

                    【讨论】:

                      【解决方案16】:

                      我认为 CodeIgniter 3 建议手动将$config['base_url'] 设置为完整的 url 以避免HTTP header injection

                      如果你不关心它,你可以简单地在你的

                      中添加以下行

                      application/config/config.php

                      defined('BASE_URL') OR define('BASE_URL', (is_https() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) . '/');
                      $config['base_url'] = BASE_URL;
                      

                      通过这种方式,您还可以在所有项目代码(包括视图)中使用 BASE_URL 常量,并且您不必使用函数:

                      config_item('base_url') //returns BASE_URL
                      $this->config->item('base_url'); //returns BASE_URL
                      

                      【讨论】:

                      • 使用 SERVER_NAME 而不是 HTTP_HOST 这样更安全
                      • @kodmanyagha AFAIK 不正确。在许多情况下,SERVER_NAME 只是从 HTTP_HOST 填充的。为了获得最大的安全性,唯一的方法是检查允许的主机名,请参阅stackoverflow.com/a/1459794/260080
                      【解决方案17】:

                      在您的配置文件中,留下$config['base_url'] = ''

                      base_url 会自动创建一个。在你的 Codeigniter system/core/Config.php

                              // Set the base_url automatically if none was provided
                              if (empty($this->config['base_url']))
                              {
                                  if (isset($_SERVER['SERVER_ADDR']))
                                  {
                                      if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE)
                                      {
                                          $server_addr = '['.$_SERVER['SERVER_ADDR'].']';
                                      }
                                      else
                                      {
                                          $server_addr = $_SERVER['SERVER_ADDR'];
                                      }
                      
                                      $base_url = (is_https() ? 'https' : 'http').'://'.$server_addr
                                          .substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])));
                                  }
                                  else
                                  {
                                      $base_url = 'http://localhost/';
                                  }
                      
                                  $this->set_item('base_url', $base_url);
                              }
                      
                      

                      此代码已用于自动创建基本 URL 和协议。

                      谢谢。

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 1970-01-01
                        • 2023-01-18
                        • 2016-06-18
                        • 1970-01-01
                        • 2017-07-28
                        • 1970-01-01
                        • 1970-01-01
                        • 2018-02-10
                        相关资源
                        最近更新 更多