How to set Dynamic base url to https in CodeIgniter? - php

I try the use a dynamic base url in this post:
Set Dynamic Base Url in CodeIgniter
But I used to be use the http, but now, I would like to change to https, how can I do so? Thanks.

In your config/config.php, try this:
$root=(isset($_SERVER['HTTPS']) ? "https://" : "http://").$_SERVER['HTTP_HOST'];
$root.= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;

$config['base_url'] = (isset($_SERVER['HTTPS']) ? "https://" : "http://").$_SERVER['HTTP_HOST'];
this works for me with virtualhost setup.

You can use codeigniter hooks in pre_controller you just change the base_url http to https by string replace and set the base url

simply use this $config['base_url'] = 'https://' . $_SERVER['HTTP_HOST'] . str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

Related

change hyper link address from old domain to new domain

i have uploaded my CodeIgniter website to another domain. i dont want my old domain anymore. problem is that i cant change manually every hyperlink from old to new
like
www.oldedomain.com/home
www.oldedomain.com/about
i want to change automatically all the pages link to new domain name
www.newdomain.com/home
www.newdomain.com/home
for all pages in website.
plz provide me best solution.
You can specify the base_url of your website in the config.php file of your codeigniter 3 config:
$config['base_url'] = 'https://www.newdomain.com';
or you can use the following lines and it will automatically detect your domain:
$root=(isset($_SERVER['HTTPS']) ? "https://" : "http://").$_SERVER['HTTP_HOST'];
$root.= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
If you are using CI4, use the following code in your Constants file:
$root=(isset($_SERVER['HTTPS']) ? "https://" : "http://").$_SERVER['HTTP_HOST'];
$root.= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
defined('BASEURL') || define('BASEURL',$root);
and in your App.php file:
public $baseURL = BASEURL;
You will also have to change the server level config in your Apache/Nginx vhost files.

Set Dynamic Base Url with Condition in Config Codeigniter

I need to set a dynamic base URL in Codeigniter due to few following reasons.
If IP using 10.3.1.77 change into HTTPS:
$config['base_url'] = "https://".$_SERVER['HTTP_HOST'].
str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
Else, change into HTTP:
$config['base_url'] = "https://".$_SERVER['HTTP_HOST'].
str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
But I have no idea how to identify the IP.
Any suggestions will be appreciated.
You can add a simple logic in config.php, remember application/config/config.php is a php script you can add any type of logic there!
try this out, edit the following code as per need.
$config['base_url'] = ($_SERVER['HTTP_HOST'] == 10.3.1.77 ? 'https' : 'http'). "://".$_SERVER['HTTP_HOST'].str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

Codeigniter redirect doubles URL

I'm using codeigniter in localhost and I'm trying to redirect my controller to another controller, so what I basically do is:
redirect('/account/index');
However, it does not go to the URL, instead it goes here:
http://localhost/ticketsystem/index.php/logincheck/localhost/ticketsystem/index.php/account/index
It doubles my address. Do I need to set something in my config.php? My setup in my config.php is
$config['base_url'] = 'localhost/ticketsystem';
$config['index_page'] = 'index.php';
$config['uri_protocol'] = 'AUTO';
Do I need to change something there, or am I doing something else that makes my redirect cause problems?
The base_url behaving like relative path, change into complete url, and also add '/' in last
$config['base_url'] = 'localhost/ticketsystem';
To
$config['base_url'] = 'http://localhost/ticketsystem/';
your base url should be looks like
$config['base_url'] = 'http://localhost/ticketsystem/';
use redirect like :-
redirect('account/index', 'refresh');
refresh will use meta refresh and should quick redirect
try removing the '/' before account!
redirect('account/index');

CodeIgniter unusual base URL

I want to set the base URL of my CodeIgniter installation to be localhost/ci/ with the trailing slash as advised in the documentation.
I try this:
$config['base_url'] = 'http://localhost/ci/';
And my pagination links are not what I would have expected. Basically,they are broken.
I, however, try this:
$config['base_url'] = 'http://localhost/ci/index.php/';
with this set
$config['index_page'] = 'index.php';
and my pagination links are now good. Is this,
$config['base_url'] = 'http://localhost/ci/index.php/';
the correct way of writing the base URL?
Remember one thing... Your base URL should be like
$config['base_url'] = 'http://localhost/ci/';
And your index URL will be
$config['index_page'] = 'index.php';
Then your site URL will be like
"http://localhost/ci/index.php"
And if you set the index URL empty like
$config['index_page'] = '';
then your site URL will be
"http://localhost/ci/"
So better at your paginations or anywhere you better use the site URL. You can get the site URL like:
echo site_url();
The site URL will be the combination of the base URL and the index URL:
site_url = base_url + index_url;

How to set proper codeigniter base url?

when I had my site on development environment - it was url: testurl.com
Now on production server my codeigniter app's address has to be 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.
It has to be someurl.com/mysite/home/test - How to fix it? I did set
$config['base_url'] = someurl.com/mysite/
Base URL should be absolute, including the protocol:
$config['base_url'] = "http://somesite.com/somedir/";
If using the URL helper, then base_url() will output the above string.
Passing arguments to base_url() or site_url() will result in the following (assuming $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
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'])).'/';
Well that's very interesting, Here is quick and working code:
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 ROCKS!!! :)
If you leave it blank the framework will try to autodetect it since version 2.0.0.
But not in 3.0.0, see here: config.php
I think CodeIgniter 3 recommends to set $config['base_url'] to a full url manually in order to avoid HTTP header injection.
If you are not concerned about it, you can simply add the following lines in your
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_URL constant available in all your project code (including the views) and you don't have to use functions:
config_item('base_url') //returns BASE_URL
$this->config->item('base_url'); //returns BASE_URL
After declaring the base url in config.php, (note, if you are still getting the same error, check autoload.php)
$config['base_url'] = "http://somesite.com/somedir/";
Check "autoload"
CodeIgniter file structure:
\application\config\autoload.php
And enable on autoload:
$autoload['helper'] = array(**'url',** 'file');
And it works.
this is for server nd live site i apply in hostinger.com and its working fine
1st : $config['base_url'] = 'http://yoursitename.com'; (in confing.php)
2) : src="<?=base_url()?>assest/js/wow.min.js" (in view file )
3) : href="<?php echo base_url()?>index.php/Mycontroller/Method" (for url link or method calling )
application > config > config.php
search for $config['base_url'] and put your site like "//example.com" (skip protocol)
$config['base_url'] = "//someurl.com/";
This works for me.
You may use default base URL path for any server
That have to used in config folder > config.php files
replace you :
$config['base_url'] = 'index.php';
by this code:
$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
In your config file, leave the $config['base_url'] = ''
base_url will automatically create one. In your 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);
}
This code is already there to automatically create the base URL and protocol.
Thanks.
Do you miss Double quotation marks?
$config['base_url'] = "someurl.com/mysite"
Dynamic Base Url (codeigniter)
Just overwrite the line in config/config.php with the following:
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/';
$config['base_url'] = "http://".$_SERVER['SERVER_NAME']."/project_name/";
this way you config you base_url , then won't worry about in hosting. both works in localhost and server.
Base url set in CodeIgniter for all url
$config['base_url'] = "http://".$_SERVER['HTTP_HOST']."/";
Base URL should be absolute, including the protocol:
$config['base_url'] = "http://".$_SERVER['SERVER_NAME']."/mysite/";
This configuration will work in both localhost and server.
Don't forget to enable on autoload:
$autoload['helper'] = array('url','file');
Then base_url() will output as below
echo base_url('assets/style.css'); //http://yoursite.com/mysite/assets/style.css
Change in config.php like this.
$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;
Try to add the controller to your index page aswell. Just looked at as similar structure on one of my own site and I hade my base url set to = "somedomain.com/v2/controller"

Categories