当我的网站在开发环境下时,它的url是:testurl.com
现在,在生产服务器上,我的codeigniter应用程序的地址必须为someurl.com/mysite/
我将它移到那里,并且每次我尝试运行一些函数(例如/home/test),它都把我带到someurl.com/home/test这个错误的地址。
它必须是someurl.com/mysite/home/test。如何修复它?我已经设置了
$config['base_url'] = someurl.com/mysite/
当我的网站在开发环境下时,它的url是:testurl.com
现在,在生产服务器上,我的codeigniter应用程序的地址必须为someurl.com/mysite/
我将它移到那里,并且每次我尝试运行一些函数(例如/home/test),它都把我带到someurl.com/home/test这个错误的地址。
它必须是someurl.com/mysite/home/test。如何修复它?我已经设置了
$config['base_url'] = someurl.com/mysite/
基础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
检查
配置 > 配置
CodeIgniter 文件结构
替换
$config['base_url'] = "your Website url";
随着
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= preg_replace('@/+$@', '', dirname($_SERVER['SCRIPT_NAME'])).'/';
https
检测。 - Marco Demaiopreg_replace('@/+$@',...)
来从字符串中删除前导斜杠,而 ltrim($string, '/')
是一个更简单/轻量级的选项? - mickmackusa很有趣,下面是快速且有效的代码:
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 真是太棒了!!! :)
$config ['base_url']
的完整url,以避免HTTP头注入的风险。
如果您不担心这个问题,您可以简单地在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
$config['base_url'] = "http://somesite.com/somedir/";
勾选“autoload”
CodeIgniter文件结构:
\application\config\autoload.php
并在自动加载时启用:
$autoload['helper'] = array(**'url',** 'file');
它能够正常工作。
这是针对服务器和实时网站的,我在hostinger.com上申请并已成功运行。
第一步:$config['base_url'] = 'http://yoursitename.com';
(在config.php文件中)
第二步:src="<?=base_url()?>assest/js/wow.min.js"
(在视图文件中)
第三步:href="<?php echo base_url()?>index.php/Mycontroller/Method"
(用于链接或调用方法)
您可以在任何服务器上使用默认的基本URL路径,这需要在config文件夹中的config.php文件中进行替换:
$config['base_url'] = 'index.php';
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= preg_replace('@/+$@','',dirname($_SERVER['SCRIPT_NAME'])).'/';
它可以自动从任何服务器中找到你的文件夹。
应用程序 > 配置 > config.php
搜索 $config['base_url'] 并将您的站点放置在 "//example.com"(跳过协议)
$config['base_url'] = "//someurl.com/";
这对我有效。
$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和协议。
谢谢。
if (!defined('BASEPATH')) exit('No direct script access allowed');
。这可以防止脚本自行运行,因为除了CI的前置控制器或其他脚本外,BASEPATH
常量未定义(不管在你的情况下是否为www)。2)只要在CI的前置控制器(index.php)中指定它们的路径,你就可以安全地将system
和application
文件夹移到你的Web根目录之外。基本上,你的Web根目录只包含index.php和assets。希望这有所帮助。 - Brendan