php 如何设置正确的codeigniter基本网址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11792268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:07:21  来源:igfitidea点击:

How to set proper codeigniter base url?

phpcodeigniter

提问by pawel

when I had my site on development environment - it was url: testurl.com

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

Now on production server my codeigniter app's address has to be someurl.com/mysite/

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

I moved it there, and everytime I'm trying to run some function, example /home/test- it gets me into someurl.com/home/test- which is WRONG.

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

It has to be someurl.com/mysite/home/test- How to fix it? I did set

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

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

回答by Brendan

Base URL should be absolute, including the protocol:

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

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

If using the URL helper, then base_url()will output the above string.

如果使用 URL helper,base_url()则将输出上述字符串。

Passing arguments to base_url()or site_url()will result in the following (assuming $config['index_page'] = "index.php";:

将参数传递给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

回答by Amranur Rahman

Check

查看

config > config

配置 > 配置

codeigniter file structure

代码点火器文件结构

replace

代替

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

with

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

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

回答by Neeraj Singh

Well that's very interesting, Here is quick and working code:

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

index.php

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

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 ROCKS!!! :)

CodeIgniter 摇滚!!!:)

回答by complex857

If you leave it blank the framework will try to autodetectit since version 2.0.0.

如果您将其留空,则框架将尝试从 version开始自动检测2.0.0

But not in 3.0.0, see here: config.php

但不在 中3.0.0,请参见此处:config.php

回答by Marco Demaio

I think CodeIgniter 3 recommends to set $config['base_url']to a full url manually in order to avoid HTTP header injection.

我认为 CodeIgniter 3 建议$config['base_url']手动设置为完整 url 以避免HTTP 标头注入

If you are not concerned about it, you can simply add the following lines in your

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

application/config/config.php

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;

In this way you also have BASE_URLconstant available in all your project code (including the views) and you don't have to use functions:

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

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

回答by Pradipsinh Rajput

this is for server nd live site i apply in hostinger.com and its working fine

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

1st : $config['base_url'] = 'http://yoursitename.com';(in confing.php)

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

2) : src="<?=base_url()?>assest/js/wow.min.js"(in view file )

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

3) : href="<?php echo base_url()?>index.php/Mycontroller/Method"(for url link or method calling )

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

回答by Vimel Raja

After declaring the base url in config.php, (note, if you are still getting the same error, check autoload.php)

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

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

Check "autoload"

勾选“自动加载”

CodeIgniter file structure:

CodeIgniter 文件结构:

\application\config\autoload.php

And enable on autoload:

并在自动加载时启用:

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

And it works.

它有效。

回答by Saiful Islam

You may use default base URL path for any server That have to used in config folder > config.php files replace you :

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

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

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

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

it's auto found your folder form any server

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

回答by Verde

application > config > config.php

应用程序 > 配置 > config.php

search for $config['base_url'] and put your site like "//example.com" (skip protocol)

搜索 $config['base_url'] 并把你的网站像“//example.com”(跳过协议)

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

This works for me.

这对我有用。

回答by fuyunbiyi

Do you miss Double quotation marks?

你想念双引号吗?

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

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