CodeIgniter Forums
Automatically change $config['base_url'] when site is uploaded - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Automatically change $config['base_url'] when site is uploaded (/showthread.php?tid=19277)



Automatically change $config['base_url'] when site is uploaded - El Forum - 06-02-2009

[eluser]renownedmedia[/eluser]
I made the following change to the config.php file so that when I upload a codeigniter file to any web server, it will automatically change the base_url() output. I can't really see a need to ever manually define this anyway...

Code:
$config['base_url'] = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . str_replace("index.php", "", $_SERVER['PHP_SELF']);

Anyone have any reasons this is a bad idea? It also works very well with the base href code:

Quote:<base href="<?=base_url();?>" />



Automatically change $config['base_url'] when site is uploaded - El Forum - 06-02-2009

[eluser]Thorpe Obazee[/eluser]
Code:
$config['base_url']    = ((empty($_SERVER['HTTPS']) OR $_SERVER['HTTPS'] === 'off') ? 'http' : 'https').'://'.$_SERVER['HTTP_HOST'].'/';



Automatically change $config['base_url'] when site is uploaded - El Forum - 06-02-2009

[eluser]renownedmedia[/eluser]
[quote author="bargainph" date="1244011796"]
Code:
$config['base_url']    = ((empty($_SERVER['HTTPS']) OR $_SERVER['HTTPS'] === 'off') ? 'http' : 'https').'://'.$_SERVER['HTTP_HOST'].'/';
[/quote]

Code:
$config['base_url']    = ((empty($_SERVER['HTTPS']) OR $_SERVER['HTTPS'] === 'off') ? 'http' : 'https').'://'.$_SERVER['HTTP_HOST'].str_replace("index.php", "", $_SERVER['PHP_SELF']);

I just derived mine from yours so that it works within sub-directories. Have you had servers report $_SERVER['HTTPS'] as 'off'? Mine have always not had it set when in HTTP!


Automatically change $config['base_url'] when site is uploaded - El Forum - 06-02-2009

[eluser]Thorpe Obazee[/eluser]
I don't think PHP_SELF is the correct one to use. It doesn't work for routed uris


HTTPS
Quote: Note: Note that when using ISAPI with IIS, the value will be off if the request was not made through the HTTPS protocol.



Automatically change $config['base_url'] when site is uploaded - El Forum - 06-03-2009

[eluser]renownedmedia[/eluser]
everything gets loaded initially through the top level index.php, so it should always work right.

Good call on the IIS stuff, forgot about M$ land lol


Automatically change $config['base_url'] when site is uploaded - El Forum - 07-01-2009

[eluser]paulopmx[/eluser]
How about this:

$url = $_SERVER['SCRIPT_NAME'];
$url = substr($url,0,strpos($url,".php"));
$url = substr($url,0,(strlen($url) - strpos(strrev($url),"/")));
$url = ((empty($_SERVER['HTTPS']) OR $_SERVER['HTTPS'] === 'off') ? 'http' : 'https')."://".$_SERVER['HTTP_HOST'].$url;

$config['base_url'] = $url;

Will let your app work even if its inside a subfolder, or if its not using index.php as a loader.