how to set sub-domain base url in CI? - codeigniter

In CI, currently I have base_url() as 'xxx.com'.Now as per requirements,every enterprise is treated as sub-domain with its own menu, content completely different from main site.Now for sub-domain, I want to set base_url() as 'enterprise.xxx.com'.
How am I supposed to do this dynamically.The sub-domain can be viewed via main site.

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

Related

CI 3.1.3 URL in the address bar changed but showing the same page (default controller)

I got a problem here, I just created a website using CI 3.1.3, I already add a .htaccess file and also Setup the base_url config
$config['base_url'] = $_SERVER['REQUEST_URI'];
I already create the Controller, and add it to the routes.
But whenever I click a link that redirect to another page, the url in the address bar is changed, the browser is loaded, but it still showing the same page that were set in the default controller,
I already tried to search it on the google but can't find the answer, and actually I don't know how to describe my problem is.
But please help me to solve this problem, thank you
This is what I use
$protocol = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ) ? 'https://' : 'http://';
$config['base_url'] = $protocol . $_SERVER['HTTP_HOST'] . str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']);
Does it work without .htaccess and/or route config?

Codeigniter setup multiple environment

I am about to make my website goes live, but I face a problem that after uploading the entire website to the server from local machine I will need to change the config file and upload to the server, but when I run in local machine I need to reset the config again. I was wondering is there any other way solve this issue ?
Thank you.
If you are talking about changing your base url all the time.
First place this code below on index.php
if (isset($_SERVER['HTTP_HOST'])) {
$base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
$base_url .= '://'. $_SERVER['HTTP_HOST'];
$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
} else {
$base_url = 'http://localhost/';
}
define('HTTP_SERVER', $base_url);
unset($base_url);
And then in the config.php
$config['base_url'] = HTTP_SERVER;
There is no shortcuts for this. It means when your project is in local you have to setup local, when live you have to setup for live.
Explanation and Tips
At the first time you code with project in local. So you have to
setup it correctly. Cz its will be same on live hosting.
So first time when you move all the files and folders to Live Host
you have to change, Database Configs, BaseUrl..
Then if you edit some thing on your localhost and you need to live
the site, Just move Controller, View, Model folders only. Do not move
the Config Folder(if any change do it manually).
So each and every time when you move the site, your config will be remain same at it is.
When you moving site to live, in root(outside application folder) edit index.php with following.
Change this to
define('ENVIRONMENT', 'development'); # shows all the run time errors
This
define('ENVIRONMENT', 'production'); # hide those and run the web smoothly, unless 500 or 404

Codeigniter url changes from 'localhost' to 'http://127.0.0.1/'

https://stackoverflow.com/a/29562910/4713946
This is my problem, and I am not sure about the answer. I am unsure where I put this code. I cannot comment on the post itself as I do not have enough reputation so I have to start a new thread. /:
Can someone maybe help me out.
I do a redirect() and when I do this, the 'Localhost' changes to '127.0.0.1'
So turns out all I had to do was change 'base_url' in 'config.php' file:
$config['base_url'] = '';
to
$config['base_url'] = '/';
You should set your base url if your using codeigniter 3 versions
$config['base_url'] = 'http://localhost/yourproject/';
If base url is blank it codeigniter 3 versions will show ip_address in url instead of localhost.

Https (SSL) in Codeigniter not working properly

I have a CI site with several form using jquery .click function, when I was in http its worked well, when I change to https all the form click button cannot be fire, its happen in localhost and in web host as well, is that anything need to config to run CI in https?
please advise and thanks!
Solved:
I just remove the url from $config['base_url'], and now the issue is solved, but I wonder how come when running https couldn't set value on $config['base_url']? hope someone would clear my doubt.
Thanks guy for taking time to view my question.
Set your base_url protocol independent:
$config['base_domain'] = 'yourdomain.com'; // a new config item if you need to get your domain name
if (isset($_SERVER['HTTP_HOST']))
{
$protocol = ($_SERVER['SERVER_PORT'] == 443 ? 'https://' : 'http://');
$config['base_url'] = $protocol.$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
}
else
{
$config['base_url'] = '';
}
I am guessing your ajax request fails because you are trying to access no-secure content from a secure site.
I had a similar issue. I simply changed the base URL from HTTP to HTTPS in the config file and it worked well for both protocols.
# Base URL in codeigniter with HTTP
$config['base_url'] = 'http://mysite.abc/';
# Base URL in codeigniter with HTTPS
$config['base_url'] = 'https://mysite.abc/';
Remember, when you change HTTP to HTTPS, the site should work well for both protocols but it doesn't work the other way around.

Codeigniter portnumber in url

im using code igniter current version .I moved all the files to the live server
i access URl as http://xxx:8084/application/
My class does not seem to load ,$this also does not work
It stops executing before parent::controller ,i get call to a function model on non object
Make sure to set the correct base URL in application/config/config.php. A generic solution that should work in most cases is:
$config['base_url'] = "http://" . $_SERVER['HTTP_HOST'] . str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);

Resources